-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Persisting one-to-many object
PostPosted: Wed Jun 07, 2006 4:16 am 
Newbie

Joined: Thu Jun 01, 2006 2:06 am
Posts: 5
Hi,

I'm using <sql-insert> statement to store the 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:
----------
<sql-insert> from the child.hbm.xml is not executed, insted hibernate generates update query on its own since USERNAME exists.

USERNAME - is the foreign key in CHILD_TABLE.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:26 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Have a look at the following link: -

http://www.hibernate.org/hib_docs/v3/re ... ild-update

You have a case where the id is assigned by the user before save similar to the case of composite id. When the child is passed to saveorupdate it would always get updated. If the id was generated, then an unsaved value say 'null' for the id would tell Hibernate that this is a new object and it should be inserted.

You could either change the way you are setting the id for the child or use the timestamp or version property.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 12:39 am 
Newbie

Joined: Thu Jun 01, 2006 2:06 am
Posts: 5
Yes,

When we don't have value for the primary key of child table, its calling the insert statement. Now I'm using sequence to fill the value for the primary key column in child table.

Thanks Jayesh......


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.