-->
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.  [ 11 posts ] 
Author Message
 Post subject: Problem saving set - object references an unsaved transient
PostPosted: Tue Jan 13, 2009 2:19 pm 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
I have a class 'ServiceLinkInstruction' that includes as a property set of subordinate 'InstructionStep' classes, like...

Code:
<set name="steps" lazy="false" >
   <key column="LinkIntID"/>
   <one-to-many class="com.eis.service.link.InstructionStep" not-found="ignore"/>
</set>


However, when I try to save an instance of the main 'ServiceLinkInstruction' class I get the following error...

Code:
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.eis.service.link.InstructionStep; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.eis.service.link.InstructionStep
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:636)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:690)
   at com.eis.service.link.ServiceLinkDAOImpl.addEntryLink(ServiceLinkDAOImpl.java:79)
   ... 11 more


Any ideas??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2009 4:20 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Did you add an InstructionStep to the set before you got that error?

try either
Code:
InstructionStep s = new InstructionStep();
...
session.save(s);
ServiceLinkInstruction sli = new ServiceLinkInstruction();
...
sli.getSteps().add(s);
session.save(sli);
..
session.getTransaction().commit();

or

map the set with cascade="all" (or at least "save")


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2009 5:14 pm 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
I added 'cascade' and I got a new problem, see...
Code:
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`skap/instruction_step`, CONSTRAINT `fk_instruction_step_service_link` FOREIGN KEY (`LinkIntID`) REFERENCES `link_definition` (`LinkIntID`) ON DELETE CASCADE ON UPDATE NO ACTION)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2934)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
   at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
   at com.mysql.jdbc.Connection.execSQL(Connection.java:3255)
   at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1566)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1480)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1465)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
   at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
   ... 50 more


Then I removed the foreign key constraint and it works OK.

As a relative Hibernate newbie, is there something I can read that explains how mapping collections works with database foreign key definitions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2009 7:12 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Don't know if its in the manual, were those constraints predefined or generated with the schema?

Maybe something is wrong in your mapping, otherwise you might have to save the other object first and then add it to the collection to please the constraint


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 3:38 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
davout_uk wrote:
...

As a relative Hibernate newbie, is there something I can read that explains how mapping collections works with database foreign key definitions?


6. Collection Mapping
7. Association Mappings


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 3:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It sounds like you have come across the issue that is discussed in one of the examples in the Hibernate docs: http://www.hibernate.org/hib_docs/v3/re ... bidir.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 12:30 pm 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
I've reworked my parent child mappings based upon the patterns used in the suggested reading.

Child mapping...
Code:
<hibernate-mapping>
    <class name="com.eis.service.link.InstructionStep" table="instruction_step">
        <id name="ID" type="java.lang.Integer" unsaved-value="-1">
            <column name="StepIntID" />
            <generator class="native" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="StepTitle" length="50" />
        </property>
        <property name="displayOrder" type="java.lang.Integer">
            <column name="StepDisplayOrder" />
        </property>
        <property name="message" type="java.lang.String">
            <column name="StepMessage" length="65535" />
        </property>
        <many-to-one name="parent" column="LinkIntID" not-null="true"/>
    </class>
</hibernate-mapping>


Parent mapping...
Code:
<set name="steps" lazy="false" inverse="true" cascade="all" order-by="StepDisplayOrder asc">
   <key column="LinkIntID"/>
   <one-to-many class="com.eis.service.link.InstructionStep"/>
</set>


This seems to work OK, except for one thing - deleting child entries.

If I first save a parent with three child entries, and then update the parent to have only 1 child then the two redundant child entries are not being deleted. Retrieving the parent will return an instance with 3 child entries.

Any ideas?


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 2:27 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think you should use cascade="all-delete-orphan". cascade="delete" (or cascade="all") only cascades if you delete the parent.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 2:53 pm 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
If I change cascade to "all-delete-orphan" then the following exception is generated..

Code:
Cause: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.eis.service.link.ServiceLinkInstruction.steps; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.eis.service.link.ServiceLinkInstruction.steps
org.springframework.orm.hibernate3.HibernateSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.eis.service.link.ServiceLinkInstruction.steps; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.eis.service.link.ServiceLinkInstruction.steps
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:661)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
   at com.eis.service.link.ServiceLinkDAOImpl.doFindByEntry(ServiceLinkDAOImpl.java:152)
...


This error occurs when I try to retrieve the parent object.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 3:17 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
From what I can tell by the error message and by looking at the code in Hibernate where this is thrown, I think this happens if you replace the 'steps' collection with a different one in your class. The collection that Hibernate is passing to ServiceLinkInstruction.setSteps() must be the same collection that you return from ServiceLinkInstruction.getSteps().


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 3:32 pm 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
Well spotted....

I had code in the 'SetSteps(<Set> steps)' method that copied the incoming set rather taking it as an assignment.

This now works fine - many thanks for your asistance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.