0

I am lil bit confused in hibernate,

Here i am having one parent object and this parent object (Suppose A is a parent and B is child object) has many to one unidirectional relation with child object B.

Now, I have a object A along with Object B . What i have done is , i saved all the child objects in database, so now every child objects are saved in db and have id .

Now i want to save parent object A for its related child object. How can i do that ? Though it might be very simple for u guys, i am new to hibernate. so need lil help.

Thanx in advance. :)

user3407459
  • 31
  • 1
  • 8
  • If the Hibernate relationships are confusing you, have a look at the help Q&A that I wrote [here](http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-x-relationship-in-hibernate-4-and-spring) – JamesENL Jun 22 '14 at 03:28

1 Answers1

1

Why are you saving parent and child separately? Set the child to parent and merge the parent. Underlying ORM technology will take care of rest. But just add cascade type PERSIST and MERGE (ideally PERSIST is enough, but MERGE handles updates as well) over the relationship in parent entity.

HJK
  • 1,382
  • 2
  • 9
  • 19
  • but as per my requirement, i have xml file which i am converting to java object . now if i do as u said then there will be multiple records in child table as relationship is many to one. this leads to data duplication. thats y i have saved child object first and need to save parent now. how can i do that? – user3407459 Jun 21 '14 at 15:22
  • Are you facing any issues while persisting the parent? Simply persist the parent. But make sure that don't use cascade types MERGE, ALL or PERSIST. Otherwise as the child object is NULL in the parent, it tries to delete the child from database. If you need those cascade types, there is no other go, fetch the child, set it to parent and merge the parent. – HJK Jun 21 '14 at 15:29
  • fetching the child object which will be having id , and setting these child objects in parent object which does not have a ids, i want to know that how does hibernate saves parent object in this scenario? – user3407459 Jun 21 '14 at 15:37
  • Sorry. I forgot to ask you one question. How you save the child in the database without parent. Don't you have referential integrity in database? The approach what you are following is wrong. Construct the proper child object from the XML, before persisting it to DB, why don't you set it to parent and persist the parent. – HJK Jun 21 '14 at 15:54
  • First i convert the whole xml to java object along with child inside the parent, then i get the each unique child object from the every parent object and save it to database, because saving the whole parent object will lead to duplicate records in database as relationship is many to one. Now i have saved the child i want to save the parent which should have reference id of child in database. and i am this because i dont want duplicate records in database. – user3407459 Jun 21 '14 at 16:19
  • Doesn't each parent has child? I don't understand about duplicate records. What I am saying is instead of saving child to database, get the unique object of child from parent, de-reference the child in parent, set the new unique child object to parent and merge the parent to database. Get the unique child from parent -> set that unique child to parent (to what ever parent the child is mapped to) -> now merge the parent. – HJK Jun 22 '14 at 01:19