Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In JPA OneToOne relationship how to insert only one side of the relation

 
Jones Gold
Greenhorn
Posts: 1
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends
I want to insert data in one side of the relation

This is my Parent entity





This is my Child entity




Remote Interface Implementation





This is My Insertion Class Save method



When I call Save matheod
MY DATABASE SHOWING RESULT AS
Parent Table In this is primary key

id pfirstName parentAge plastName
1 GOL 1 JON

Child Table
In this Child Table ID is primary key and pid is foreign key which refers to Parent Table Id
id cfirstName childtAge clastName pid
1 jj 1 gg 1

Now I Want to insert only Child Table like below
id cfirstName childtAge clastName pid
1 jj 1 gg 1
2 kk 2 oo 1

How it possible?


 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you want to insert another child of the same parent.

Since the parent is existing, you should first find() the existing parent, then create a new Child with that parent and persist it.

If you are dealing with a detached child (serialize from remote), then you can call merge() instead of persist, and JPA should figure out it is new and the parent is existing (assuming the id is set in the parent).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic