7

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");
p.addChild(c);
session.flush();
  • 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

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70

4 Answers4

5

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();
child.setParent(parent);
session.save(child);

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

Hope this help.

Nikunj
  • 3,100
  • 2
  • 20
  • 19
  • 2
    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`. – Stefan Steinegger Jun 21 '11 at 14:53
  • @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 Jun 22 '11 at 06:04
  • so no need to add the session.save(child), as cascade will already handle that – TimeToCodeTheRoad Jun 22 '11 at 06:24
  • is that right? i mean there is no need to add sessin.save(child) – TimeToCodeTheRoad Jun 22 '11 at 06:56
  • @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? – Nikunj Jun 22 '11 at 07:12
  • @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. – Stefan Steinegger Jun 23 '11 at 10:26
  • 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. – Nikunj Jun 23 '11 at 10:45
3

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.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
1

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.

user3094331
  • 532
  • 6
  • 21
0

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.

Sanjeev Singh
  • 51
  • 1
  • 6