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 Child record not saved in one-to-many hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Child record not saved in one-to-many hibernate" Watch "Child record not saved in one-to-many hibernate" New topic
Author

Child record not saved in one-to-many hibernate

NItin Sehra
Greenhorn

Joined: May 31, 2006
Posts: 4
I am trying to do a simple parent child save assuming hibernate will insert child record when i will call save on parent.

Parent xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping default-cascade="none" default-access="property" auto-import="true">
<class name="com.freightdesk.intelliagent.model.ControlAgentModel" table="IA_CONTROLAGENT">
<id name="controlAgentId" type="long">
<column name="CONTROLAGENTID" sql-type="number" not-null="true"/>
<generator class="sequence">
<param name="sequence">IA_CONTROLAGENTID</param>
</generator>
</id>
<property name="controlAgentName">
<column name="CONTROLAGENTNAME" />
</property>
<property name="controlAgentDesc">
<column name="CONTROLAGENTDESC" />
</property>
<property name="defaultAlertSubject">
<column name="DEFAULTALERTSUBJECT" />
</property>
<property name="defaultAlertTemplate">
<column name="DEFAULTALERTTEMPLATE" />
</property>
<property name="status"/>
<property name="lastUpdateUserId"/>
<property name="lastUpdateTimestamp"/>
<property name="createUserId"/>
<property name="createTimestamp" insert="false" />
<property name="domainName"/>
<property name="lastExecutedTimestamp">
<column name="LASTEXECUTEDTIME" />
</property>
<bag name="alertList" cascade="all-delete-orphan" inverse="true" >
<key> <column name="CONTROLAGENTID" not-null="true" /></key>
<one-to-many class="com.freightdesk.intelliagent.model.AlertActionModel"/>
</bag>
</class>
</hibernate-mapping>


Child xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping default-cascade="none" default-access="property" auto-import="true">
<class name="com.freightdesk.intelliagent.model.AlertActionModel" table="IA_ALERT">
<id name="intelliAgentAlertId" type="long">
<column name="ALERTID" sql-type="number" not-null="true"/>
<generator class="assigned">
<param name="sequence">IA_ALERTID</param>
</generator>
</id>
<property name="agentID" not-null="true">
<column name="CONTROLAGENTID" />
</property>
<property name="intelliAgentAlertTypeCode">
<column name="ALERTTYPECODE" />
</property>
<property name="intelliAgentAlertTarget">
<column name="ALERTTARGET" />
</property>
<property name="intelliAgentAlertSubject">
<column name="ALERTSUBJECT" />
</property>
<property name="intelliAgentAlertTemplate">
<column name="ALERTTEMPLATE" />
</property>
<property name="intelliAgentAlertPriority">
<column name="PRIORITY" />
</property>
<property name="allowRichText">
<column name="ALLOWRICHTEXT" />
</property>
<property name="useDefaults">
<column name="USEDEFAULTS" />
</property>
<property name="status"/>
<property name="lastUpdateUserId"/>
<property name="lastUpdateTimestamp"/>
<property name="createUserId"/>
<property name="createTimestamp" insert="false" />
<property name="domainName"/>
<many-to-one name="controlAgent" not-null="true" column="CONTROLAGENTID" insert="false" update="false" />

</class>
</hibernate-mapping>

Now when i do a simple save the parent get created but child record are not inserted.
The Hibernate debug show:
Insert on parent
Update on child

Please help me out , its been hours i m digging this problem
Tina Coleman
Ranch Hand

Joined: Dec 12, 2001
Posts: 150
Verify: did you assign the alertId for the child? I see that it's an assigned id. Given that your debug message claims that it's an 'Update on child', that suggests that you're reusing your previous child based on reusing an assigned id, rather than creating a new one.

Can you generate any of the hibernate-specific logs?
NItin Sehra
Greenhorn

Joined: May 31, 2006
Posts: 4
No i am not assining alertid ,the alertid is coming from a database sequnce,
i just modifed my child xml as

<id name="intelliAgentAlertId" type="long">
<column name="ALERTID" sql-type="number" not-null="true"/>
<generator class="sequence"> <param name="sequence">IA_ALERTID</param>
</generator>
</id>

ANd get the Hibernate debug as :
Hibernate: select IA_CONTROLAGENTID.nextval from dual

Hibernate: insert into IA_CONTROLAGENT (CONTROLAGENTNAME, CONTROLAGENTDESC, DEFAULTALERTSUBJECT, DEFAULTALERTTEMPLATE, status, lastUpdateUserId, lastUpdateTimestamp, createUserId, domainName, LASTEXECUTEDTIME, CONTROLAGENTID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: update IA_ALERT set CONTROLAGENTID=?, ALERTTYPECODE=?, ALERTTARGET=?, ALERTSUBJECT=?, ALERTTEMPLATE=?, PRIORITY=?, ALLOWRICHTEXT=?, USEDEFAULTS=?, status=?, lastUpdateUserId=?, lastUpdateTimestamp=?, createUserId=?, createTimestamp=?, domainName=? where ALERTID=?

I don't understand y hibernate is not inserting IA_ALERT record first.
i have done show-sql true ,is there is other verbose option avilable for debugging hibernate.
Thanks for reply
Tina Coleman
Ranch Hand

Joined: Dec 12, 2001
Posts: 150
If you're using log4j, you can add the following properties to your log4j.properties file to get some additional information:
NItin Sehra
Greenhorn

Joined: May 31, 2006
Posts: 4
I got this error

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0

Any idea would be of great help
Tina Coleman
Ranch Hand

Joined: Dec 12, 2001
Posts: 150
My first check would be to see if your sequence exists in the database, and verify that it is indeed incrementing appropriately.
NItin Sehra
Greenhorn

Joined: May 31, 2006
Posts: 4
I will reiterate my problem :
Parent Class mapping :
<set name="test" inverse="true" cascade="all" >
<key column="CONTROLAGENTID" />
<one-to-many class="com.freightdesk.intelliagent.model.DBActionModel" />
</set>

Child Class mapping :
<many-to-one name="controlAgent"
class="com.freightdesk.intelliagent.model.ControlAgentModel"
column="CONTROLAGENTID" insert="true" update="true" />

Java Code :
DBActionModel dbModel = new DBActionModel();
dbModel.setControlAgent(controlAgentModel);
controlAgentModel.getTest().add(dbModel);
session.save(controlAgentModel);

The Hibernate excute first insert on parent and then try to update child




Any help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Child record not saved in one-to-many hibernate