0
votes

I'd like to know what really happens when I do:

Person p1 = new Person();
...
p1.save()

in terms of cascades order of save-update.

Here there're some reasonings that I'd like to know if they're correct:

a) If Person have at least one relationship with 'inverse=false' (default), it will do all the cascades first and finally persist p1 itself. (Because it could be that the FK identity is not yet known)

b) If Person have all relationships with 'inverse=true', it could save p1 first and then do the cascades.

c) Suppose we have a Person<-->Car one-to-one bidirectional relationship and in both we have 'inverse=false' and 'save-update' cascade:

c.1) What would happen?. It seems like a infinite loop situation.

c.2) The correct solution is in Person doing 'cascade=save-update' with inverse=true, and in Car not doing cascade and inverse=false?

Maybe further links about the real logic behind the decision of the order of doing cascades and how the 'inverse' property is involved could help me.

Thanks in advance

1

1 Answers

1
votes

In cascade, after one operation (save, update and delete) is done, it decide whether it need to call other operations (save, update and delete) on another entities which has relationship with each other.

so for the problem a and b cascade will always checked(not always happen) after the initial process is done, if cascade still needed then hibernate will do cascade, if not it's not performed.

for problem C, one-to-one and many-to-one doesn't have inverse keyword, so it will be ignored. But if you want to create the correct bidirectional one-to-one, you can see it here.