hi,
when we establish a parent child relationship, then normally the FK in the child table will be the PK of the parent table.
and in this case, i can map the relationship in the mapping file as follows:
The parent would map to the child as:
Code:
<id name="parentid" type="int" column="PARENTID"/>
<set name="children" inverse="true" cascade="all-delete-orphan"
sort="unsorted" mutable="true" optimistic-lock="true" embed-xml="true"> <key column="CHILDID" on-delete="noaction"/>
<one-to-many class="edu.nyu.hibernate.Child" not-found="exception" embed-xml="true"/>
</set>
The child would map to the parent as:
Code:
<id name="childid" type="int" column="CHILDID"/>
<many-to-one name="parent" class="edu.nyu.hibernate.Parent" column="PARENTID" insert="false" update="false" unique="false"
optimistic-lock="true" not-found="exception" embed-xml="true"/>
This I have verified that it works fine, but problem occurs when the FK in the child table is not the PK of the parent table.
In the hibernate documentation, the way to get around this is to use:
Code:
property-ref="propertyName"
which according to the documentation "Specifies that the foreign key refers to columns that are not the primary key of the orginal table. (Provided for legacy data.) "
My understanding is that I need to put this only in the child mapping file, but is this correct? Because when I do so, I get a java.sql.BatchUpdateException stating that:
insert into CHILD (column1, column2, ... ) values (value1, value2, ...) was aborteed. Call getNextException to see the cause.
Any guidance with this will be very helpful. thanks.