File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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
Author

Persisting one-to-many object using sql-insert

Manikandan Pandurangan
Greenhorn

Joined: May 31, 2006
Posts: 5
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

Joined: May 16, 2005
Posts: 513

Did you map the relation for <many-to-one>?
child.setEncryptedData(encrytedData);


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

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Persisting one-to-many object using sql-insert