Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Persisting one-to-many object using sql-insert

 
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.
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • 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.
 
    Bookmark Topic Watch Topic
  • New Topic