Friday, May 21, 2010

Hibernate : One-to-many Parent-Child relationships...

Hibernate is quite good ORM framework. When we need one-to-many relationship in hibernate, we need to take ArrayList or Set. If the structure is kind of tree, then hibernate can perform wired if not properly tweaked.


Basically if you are having some sort of many to one relationships structure from child to parent. In hibernate all depends on mapping. Tweak your mapping, Use One-to-many relationship from parent to child using java.util.Set.


Do not use ArrayList because List is ordered, so hibernate will add extra column for that ordering only.
Also check your lazy property. If you load parent and you have set lazy="false" on its child set property (variable which is of type Set), then all of its children will be loaded from DB which can affect the performance.


Also check 'inverse' property for children. If inverse is true in child table, that means you can manage the child entity separately. Otherwise you have to do that using the parent only.

0 Comments: