Reputation: 23587
With respect to my previous post Previous Post
I have a Parent table A and its Child table B. Relation of A to B is one-to-many where as of that of B to A is many-to one i need to fetch the data from table B based on its parent id and for which i was trying to use named query here is the Updated hbm file for the child table.
<hibernate-mapping>
<class
name="ChildClass"
table="B">
<id name="uuid" type="java.lang.String">
<column name="UUID" />
<generator class="uuid" />
</id>
<!-- <version name="version" column="OBJ_VERSION" /> -->
<many-to-one name="A"
class="ParentClass" fetch="join">
<column name="PARENTID" />
</many-to-one>
<property name="parentid" type="java.lang.String" update="false" insert="false">
<column name="PARENTID" />
</property>
</class>
</hibernate-mapping>
Starting idea to add
<property name="parentid" type="java.lang.String" update="false" insert="false">
<column name="PARENTID" />
</property>
was to make use of hibernate named query based on parentid.Something like this
<hibernate-mapping>
<query name="getChildRecordsByParentID">
from Child child where child.parentid = :parentid
</query>
</hibernate-mapping>
and i was able to achieve this but struck in a major problem.Sometime i want to update the child table based on the parentid so i was just fetching the parent object based on the parentid and was using its reference in child entity and was trying to save the child instance,everything was getting saved properly except the parentid which is being set as "null"
i think null
is being set as i have made the parentid as read only
i am just a starter in Hibernate so not sure where exactly i am doing wrong in mapping since changing to mapping file is causing the second functionality to stop working.
Any help in this regard will be much helpful.
Upvotes: 0
Views: 2103
Reputation: 23587
That was some stupid mistake from my side or honestly lack of debugging.Able to solve the problem since the id based on which i was fetching my parent was wrong and hence null was being sent.
Thanks Sean for the valuable inputs.
Upvotes: 0
Reputation: 8605
How are you trying to link the child with a given parent?
Since your parentId
property is update="false" insert="false"
, that property is completely ignored in terms of updating (it is used as a readonly property when update/insert is false).
Looks like you map the parent as A
, so in order to set the parent on the child, you should only need to call child.setA(parent);
, and save the child.
Upvotes: 1