my dog learned polymorphism
The moose likes Object Relational Mapping and the fly likes Persisting one-to-many object using  sql-insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Persisting one-to-many object using  sql-insert" Watch "Persisting one-to-many object using  sql-insert" New topic

Persisting one-to-many object using sql-insert

Manikandan Pandurangan

Joined: May 31, 2006
Posts: 5

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,

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();

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


<class name="com.test.EncryptedData" table="HIBERNATE_TEST" schema="iris01" lazy="false">
<id name="userName" type="java.lang.String"
<column name="USERNAME" precision="15" scale="0" />

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

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

VALUES (Toolkit.encrypt(?,'12345678'),?)

<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" />

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

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

VALUES (?,?,?)

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

Joined: May 16, 2005
Posts: 659

Did you map the relation for <many-to-one>?

If you are not laughing at yourself, then you just didn't get the joke.
Manikandan Pandurangan

Joined: May 31, 2006
Posts: 5
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.
I agree. Here's the link:
subject: Persisting one-to-many object using sql-insert
It's not a secret anymore!