Mar 9, 2011

notes on hibernate many-to-one, one-to-many

many-to-one
usage:
<many-to-one name="parentDao" class="xxx.ParentDAO" column="REF_ID" lazy="false" update="false"/>
name - data member in dao class
column - foreign key of child dao in database
class - package + class name in java project
lazy - false means retrieve the corresponding parent dao anyway; true means retrieve only when needed, only works for Session.load()
update/insert - indicate whether the operation would be performed

one-to-many
usage:
<list name="childList" table="CHILD_TBL" lazy="false" cascade="all,delete-orphan">
    <key column="CT_REF_ID"/>
    <list-index column="CT_SEQ"/>
    <one-to-many class="xxx.ChildDAO"/>
</list>
name - data member in dao class: private List<childdao> childList;
lazy - false means retrieve the corresponding child dao anyway; true means retrieve only when needed, only works for Session.load()
cascade - see hibernate manual
key - foreign key of child dao in database
list-index - sequence used by list in hibernate
use <composite-element> as alternative

No comments:

Post a Comment