By TimeToCodeTheRoad

2011-06-21 10:33:35 8 Comments

I am creating a one-to-many relationship. so, i have a parent and a child. The cascade attribute is set to all.

I was wondering, if we consider the following piece of code:

Parent p = (Parent) session.load(Parent.class, pid); 
Child c = new Child("child element");
  • Q1) If the parent owns the relationship, as in , for the parent inverse=false, then would the child element addition be updated in teh database?
  • Q2) If the child owns the relationship, as in , for the parent inverse=true, then will the child element addtion be updated in the databse?
  • Q3) Who owns the relationahsip does not make a difference in the above code in terms of whether the updaet will be seen or not?

thanks a lot


@Sanjeev Singh 2016-08-02 17:51:46

In a parent child relation ship between two different entities,

e.g one to many(1:N) or many to one(N:1)

Parent <-> Child. (Owner) (Inverse)

If parent is owner then child is its inverse.

Using the inverse is always check for child.

By default we always consider from parent side. So by default inverse = false means parent is owner.

If inverse= true then child is owner. So persisting of entity will always take from owner side.

@user3094331 2016-07-20 09:14:44

If we are using inverse=true, means the child is responsible to update relation ship. Compulsory child object should contain the parent object else foreign key is not updated.

@Stefan Steinegger 2011-06-21 14:59:29

Inverse is only to tell NH that the foreign key is mapped twice, usually as a one-to-many and a many-to-one, and that it therefore only needs to be stored from one side.

Q1) the child is stored by cascade, but the parent-FK is null. (Except you set the parent relation in the child within p.addChild(c).)

Q2) same as Q1.

Q3) exactly.

@nIKUNJ 2011-06-21 11:25:01

Case inverse = false:

In this case, it is parent's responsibility to save-update child and its relationship. So in your example, child will be updated in database. There will be two sql queries: 1) Insert child. 2) Update child with foreign key of parent id.

Case Inverse = true:

In this case , it is child's responsibility to save-update itself. So in your code, child will be saved in database but foreign key of parent will be null. Only one sql query will be executed and that is of insert child. For updating parent's foreign key, you need to manually save child.

Child child = new Child();

I think, answer of these cases explains answer of your third question.

Hope this help.

@Stefan Steinegger 2011-06-21 14:53:49

Not exactly. Cascade is independent of inverse. As you said, the parent-FK will be null, but you only need to set the relation to the parent, no need to save.

@TimeToCodeTheRoad 2011-06-22 06:04:21

@Stefan: thanks for the input! so, in my code, if the child is responible for relationship, then when i do p.addChild(child), then the child will be saved in the database, but the foreign key will be null. then i will have to write the statement: child.setParent(parent) to set the foreign key. Is that right?

@TimeToCodeTheRoad 2011-06-22 06:24:42

so no need to add the, as cascade will already handle that

@TimeToCodeTheRoad 2011-06-22 06:56:48

is that right? i mean there is no need to add

@nIKUNJ 2011-06-22 07:12:38

@Stefan, you are very true. No need to save child separately.But if parent has List(not Set) of child, then there will an index column in child table for tracking child index. If inverse= true and we are not saving child separately, then that column will remain null in child table. So, next time when you get child list from parent, it will throw an exception saying null index column for collection. Am i right in this case?

@Stefan Steinegger 2011-06-23 10:26:28

@nIKUNJ: I already had this problem with the list index. I have to admit that I'm not really sure if it works and how it should be. I don't understand why it can't be updated by NH, even with inverse=true, and why explicitly saving the child should fix the problem. Probably it does.

@nIKUNJ 2011-06-23 10:45:21

I think because when inverse=true, responsibility shift to child to update parent foreign key and child does not has any idea about his and other element's index. In case of List, it first saves/updates childs and then updates the index column with extra queries. So, we can say that inverse=true won't work for indexed collection like List. Because, it has to have control over its children' save and updates.

Related Questions

Sponsored Content

13 Answered Questions

1 Answered Questions

[SOLVED] Hibernate inverse="true" saving parent saves children when cascade="all"

  • 2015-09-03 14:25:12
  • user1308908
  • 809 View
  • 0 Score
  • 1 Answer
  • Tags:   java hibernate

1 Answered Questions

1 Answered Questions

[SOLVED] Hibernate relationship between cascade and inverse

1 Answered Questions

[SOLVED] Cascade and Inverse in persistence graph

1 Answered Questions

confusion in Hibernate Inverse

  • 2012-12-18 11:45:45
  • chhaya
  • 269 View
  • 0 Score
  • 1 Answer
  • Tags:   hibernate

1 Answered Questions

What is the difference between cascade and inverse in Hibernate?

1 Answered Questions

3 Answered Questions

Sponsored Content