This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persisting one-to-many object using sql-insert

 
Manikandan Pandurangan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using <sql-insert> statement to store one-to-many relationship.
But its able to insert record on Master table only.

Instead of calling the <sql-insert> statement from the child hbm file, its generating update query on its own for the Detail table. Could any one help to fix this issue...

Thank you,
Mani

Below is the code snippet

Parent Object
----------------
public class EncryptedData implements Serializable {
private String userName;
private String dataToEncrypt;
private Set childSet = null;
}

Child Object
---------------
public class Child implements Serializable {
private String childName;
private String childData;
private EncryptedData encryptedData = null;
}

DAO Call
-----------
encryptedData = new EncryptedData();
encryptedData.setUserName("User Name Column");
encryptedData.setDataToEncrypt("Encrypted data");

Child child = new Child();
child.setChildName("C1");
child.setChildData("Name");

Set childSet = new HashSet();
for (int i=0; i<10; i++) {
childSet.add(child);
}

encryptedData.setChildSet(childSet);
hibernateSession.save(encryptedData);

EncryptedData.hbm.xml
----------------------------
<hibernate-mapping>
<class name="com.test.EncryptedData" table="HIBERNATE_TEST" schema="iris01" lazy="false">
<id name="userName" type="java.lang.String"
unsaved-value="null">
<column name="USERNAME" precision="15" scale="0" />
</id>

<set name="childSet" inverse="true" lazy="false" cascade="save-update" >
<key>
<column name="USERNAME" precision="15" scale="0" />
</key>
<one-to-many class="com.test.Child" />
</set>

<property name="dataToEncrypt" type="java.lang.String" column="DATA"/>

<sql-insert>INSERT INTO HIBERNATE_TEST(DATA, USERNAME)
VALUES (Toolkit.encrypt(?,'12345678'),?)
</sql-insert>


Child.hbm.xml
------------------
<class name="com.test.Child" table="CHILD_TABLE" schema="iris01" lazy="false">
<id name="childName" type="java.lang.String" unsaved-value="null">
<column name="CHILD_NAME" precision="15" scale="0" />
</id>

<many-to-one name="encryptedData" class="com.test.EncryptedData" column="userName" lazy="false" />

<property name="childData" type="java.lang.String" column="CHILD_DATA"/>


<sql-insert>INSERT INTO CHILD_TABLE(USERNAME,CHILD_DATA,CHILD_NAME)
VALUES (?,?,?)
</sql-insert>


Problem:
----------
After inserting record on the Master table, <sql-insert> from the child.hbm.xml is not executed, insted hibernate generates update query on its own for the child since USERNAME exists.

USERNAME - is the foreign key in CHILD_TABLE.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you map the relation for <many-to-one>?
child.setEncryptedData(encrytedData);
 
Manikandan Pandurangan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, The issue was fixed. If we have value for Child table Primary key column, then hibernate will invoke the update query.

Now I'm generation value at runtime by a sequence now.
Also by setting the timestamp/version we could avoid this issue.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic