<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Watch out for Hibernate unsaved-value</title>
	<atom:link href="http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/feed" rel="self" type="application/rss+xml" />
	<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value</link>
	<description>blogging about life, the universe, and everything tech</description>
	<lastBuildDate>Tue, 03 Jan 2012 22:08:49 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Daryl Teo</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-47561</link>
		<dc:creator>Daryl Teo</dc:creator>
		<pubDate>Tue, 09 Nov 2010 05:07:48 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-47561</guid>
		<description>Mate, you just saved me half a day of frustration! THANK YOU</description>
		<content:encoded><![CDATA[<p>Mate, you just saved me half a day of frustration! THANK YOU</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Thompson</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-47407</link>
		<dc:creator>John Thompson</dc:creator>
		<pubDate>Wed, 13 Oct 2010 00:30:30 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-47407</guid>
		<description>I got a &quot;references an unsaved transient instance&quot; error when trying to create instances of a one to many relationship. The problem was that I created all the objects and related them to each other on both sides of the one-to-many in code, but then I tried session.Save on the many side object. I guess this caused the error because it was unable to store the many side record because it had not stored the one side record yet, and therefore, it did not know the prime key value to use for the many side foreign key. Thus the error complains about an &quot;unsaved (transient) instance&quot;.
This is all a bit wordy and may not seem important, but it does cause a problem. I got the error when trying to save a Customer entity, that was related to a default address. I wanted to save the objects from the customer point of view because that is what drives the data structure from the business point of view. However, I was forced to save the customer by saving the address, and letting that cause the customer to save once the address was saved and the address key was known.
This worries me, however, because I have not been using nHibernate for long, but it looks like certain object graphs that include both one-to-many and many-to-one relations may not be saveable by nHibernate.
I don&#039;t know if this makes any sense without me showing loads of code samples and diagrams, but I would like to know if anyone has had any similar issues or concerns.</description>
		<content:encoded><![CDATA[<p>I got a &#8220;references an unsaved transient instance&#8221; error when trying to create instances of a one to many relationship. The problem was that I created all the objects and related them to each other on both sides of the one-to-many in code, but then I tried session.Save on the many side object. I guess this caused the error because it was unable to store the many side record because it had not stored the one side record yet, and therefore, it did not know the prime key value to use for the many side foreign key. Thus the error complains about an &#8220;unsaved (transient) instance&#8221;.<br />
This is all a bit wordy and may not seem important, but it does cause a problem. I got the error when trying to save a Customer entity, that was related to a default address. I wanted to save the objects from the customer point of view because that is what drives the data structure from the business point of view. However, I was forced to save the customer by saving the address, and letting that cause the customer to save once the address was saved and the address key was known.<br />
This worries me, however, because I have not been using nHibernate for long, but it looks like certain object graphs that include both one-to-many and many-to-one relations may not be saveable by nHibernate.<br />
I don&#8217;t know if this makes any sense without me showing loads of code samples and diagrams, but I would like to know if anyone has had any similar issues or concerns.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhinav</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-40826</link>
		<dc:creator>Abhinav</dc:creator>
		<pubDate>Mon, 11 May 2009 13:57:46 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-40826</guid>
		<description>Thanks..this post saved a lot of my debugging time...</description>
		<content:encoded><![CDATA[<p>Thanks..this post saved a lot of my debugging time&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Meidan</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-39855</link>
		<dc:creator>Meidan</dc:creator>
		<pubDate>Thu, 06 Nov 2008 16:33:42 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-39855</guid>
		<description>Dude! You&#039;ve probably saved me tons of debugging time after deploying to production where the was an entity with id = 0.
Thanks!</description>
		<content:encoded><![CDATA[<p>Dude! You&#8217;ve probably saved me tons of debugging time after deploying to production where the was an entity with id = 0.<br />
Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raja Nagendra Kumar</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-38761</link>
		<dc:creator>Raja Nagendra Kumar</dc:creator>
		<pubDate>Thu, 05 Jun 2008 13:18:58 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-38761</guid>
		<description>Hi zubair ,

Could pl. let me know how to set inverse attribute using annotations. I read inverse equivalent is mappedby.. however for a cose like this..

Map where DateTimeSpec is create and not saved yet but added to this map. When the class having this map instance is saved I am expecting all the new one referenced in the map of DateTimeSpec, AbstractTezzD should be saved.  Also, pl. observe that map holder is also a class which is part of inhericance chain for value of the map..


public final class TimeTezzD extends AbstractTezzD
{
@OneToMany(fetch = FetchType.EAGER,
	       cascade = CascadeType.ALL)
    @JoinTable(name = &quot;tbl_time_tezzds_map&quot;,
	       joinColumns = {@JoinColumn(name = &quot;fk_time_tezzds&quot;,
					  nullable = false)},
	       inverseJoinColumns = {@JoinColumn(name = &quot;fk_abs_tezzds&quot;,
						 nullable = false)})
    private final Map timeTezzDMap = new HashMap();

..


Workaround I see is this..

Every DataTimeSpec object I  added I save it first and then add to this map. then it is working.. however looking for jpa system to auto take care of it.. rather than giving the 

     [java] There was 1 error:
     [java] 1) testValid(com.tejasoft.dialer.model.bdo.tns.time.test.TestTimeTez
zD)org.hibernate.TransientObjectException: object references an unsaved transien
t instance - save the transient instance before flushing: com.tejasoft.dialer.mo
del.bdo.tns.time.DateTimeSpec

Any clues would help..

Regards,
Raja Nagendra Kumar,
C.T.O
www.tejasoft.com</description>
		<content:encoded><![CDATA[<p>Hi zubair ,</p>
<p>Could pl. let me know how to set inverse attribute using annotations. I read inverse equivalent is mappedby.. however for a cose like this..</p>
<p>Map where DateTimeSpec is create and not saved yet but added to this map. When the class having this map instance is saved I am expecting all the new one referenced in the map of DateTimeSpec, AbstractTezzD should be saved.  Also, pl. observe that map holder is also a class which is part of inhericance chain for value of the map..</p>
<p>public final class TimeTezzD extends AbstractTezzD<br />
{<br />
@OneToMany(fetch = FetchType.EAGER,<br />
	       cascade = CascadeType.ALL)<br />
    @JoinTable(name = &#8220;tbl_time_tezzds_map&#8221;,<br />
	       joinColumns = {@JoinColumn(name = &#8220;fk_time_tezzds&#8221;,<br />
					  nullable = false)},<br />
	       inverseJoinColumns = {@JoinColumn(name = &#8220;fk_abs_tezzds&#8221;,<br />
						 nullable = false)})<br />
    private final Map timeTezzDMap = new HashMap();</p>
<p>..</p>
<p>Workaround I see is this..</p>
<p>Every DataTimeSpec object I  added I save it first and then add to this map. then it is working.. however looking for jpa system to auto take care of it.. rather than giving the </p>
<p>     [java] There was 1 error:<br />
     [java] 1) testValid(com.tejasoft.dialer.model.bdo.tns.time.test.TestTimeTez<br />
zD)org.hibernate.TransientObjectException: object references an unsaved transien<br />
t instance &#8211; save the transient instance before flushing: com.tejasoft.dialer.mo<br />
del.bdo.tns.time.DateTimeSpec</p>
<p>Any clues would help..</p>
<p>Regards,<br />
Raja Nagendra Kumar,<br />
C.T.O<br />
<a href="http://www.tejasoft.com" rel="nofollow">http://www.tejasoft.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ray</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-37828</link>
		<dc:creator>Ray</dc:creator>
		<pubDate>Sat, 09 Feb 2008 19:53:05 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-37828</guid>
		<description>I was getting the same error.  I had a persistent parent with a transient child causing the error.  I added a cascade=&quot;all&quot; attribute to the many-to-one mapping, and presto it&#039;s fixed.</description>
		<content:encoded><![CDATA[<p>I was getting the same error.  I had a persistent parent with a transient child causing the error.  I added a cascade=&#8221;all&#8221; attribute to the many-to-one mapping, and presto it&#8217;s fixed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zubair</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-37664</link>
		<dc:creator>zubair</dc:creator>
		<pubDate>Mon, 17 Dec 2007 15:54:58 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-37664</guid>
		<description>I had this problem. I solved it by the inverse=&quot;true&quot; on the parent. 
I left the unsaved value= 0 and no issues with that. 
Parent is a one to many to the child and the child was a many to one.
Defined it in the hbml files. 
If u unsaved value is or  -1 or even 1 u can get stale exceptions.</description>
		<content:encoded><![CDATA[<p>I had this problem. I solved it by the inverse=&#8221;true&#8221; on the parent.<br />
I left the unsaved value= 0 and no issues with that.<br />
Parent is a one to many to the child and the child was a many to one.<br />
Defined it in the hbml files.<br />
If u unsaved value is or  -1 or even 1 u can get stale exceptions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Laura</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-37615</link>
		<dc:creator>Laura</dc:creator>
		<pubDate>Wed, 14 Nov 2007 13:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-37615</guid>
		<description>I encountered your site several times when I was looking for a way to fix my problem, but since i had no unsaved-value in my hibernate files, i did not think that was the problem.

However, the fact that I had no unsaved-value was the problem, because Hibernate believes that &quot;0&quot; is an unsaved-value by default. Took me a whole day to realize this and put a &quot;unsaved-value=-1&quot; in the mapping.</description>
		<content:encoded><![CDATA[<p>I encountered your site several times when I was looking for a way to fix my problem, but since i had no unsaved-value in my hibernate files, i did not think that was the problem.</p>
<p>However, the fact that I had no unsaved-value was the problem, because Hibernate believes that &#8220;0&#8243; is an unsaved-value by default. Took me a whole day to realize this and put a &#8220;unsaved-value=-1&#8243; in the mapping.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damon Murfitt</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-10855</link>
		<dc:creator>Damon Murfitt</dc:creator>
		<pubDate>Wed, 13 Jun 2007 19:01:55 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-10855</guid>
		<description>Hi, thought i would throw my thoughts at this discussion. hopefully i&#039;ll be helpfull.

I had this problem and it was related to my IInterceptor implementation.

I used a base Persister class to give nHibernate a hint to determine if a class was already in the DB.

As i was developing a web app there were occasions where i had to manually create an object using an ID. i.e Person myPerson = new Person(17765);

I then added this Person to a collection of Another object. When attempting to persist this object nHibernate checked myPerson.IsSaved which was false because i hadn&#039;t actually loaded this object from nhibernate. Because it was false nHibernate assumed it was unsaved so threw the exception being discussed (after all it didn&#039;t want to go and attempt to create an insert in my join table using the primary key of an object that didn&#039;t exists in the database).

Anyhows, changing my code to this resolved the error:
Person myPerson = new Person(17765)
myPerson.IsSaved = true;

Please note that i new a person with id 17765 already existed but i didn&#039;t want to keep going to the DB everytime i added a person to the collection. (Going to the db and getting the person would also have prevented the error. i.e myPerson = session.Get(17765); because hnibernate knows it exist in the db when i comes to save the association)

Now...Another scenario where you could get this exception would be:

I create a new Person who does not have a record in my DB:
Person myPerson = new Person(99999);
myPerson.IsSaved = false;
If I then add this person to the collection of my other object, lets call it &quot;Bus&quot;....so....
Bus.Passengers.Add(myPerson)

When nhibernate attempts to save Bus if i don&#039;t have cascade=&quot;save-update&quot; set in Bus.hbl.xml file then once again nhibernate will not want to insert into the join table using a myPerson.Id which does not exist in the Person database table.

Alternatively if i don&#039;t want to set cascade in Bus.hbl.xml then i must explicitly save myPerson so that the record does exist before nhibernate goes to insert that join record.

Phew. Rambled a bit there, hope it helps someone.
</description>
		<content:encoded><![CDATA[<p>Hi, thought i would throw my thoughts at this discussion. hopefully i&#8217;ll be helpfull.</p>
<p>I had this problem and it was related to my IInterceptor implementation.</p>
<p>I used a base Persister class to give nHibernate a hint to determine if a class was already in the DB.</p>
<p>As i was developing a web app there were occasions where i had to manually create an object using an ID. i.e Person myPerson = new Person(17765);</p>
<p>I then added this Person to a collection of Another object. When attempting to persist this object nHibernate checked myPerson.IsSaved which was false because i hadn&#8217;t actually loaded this object from nhibernate. Because it was false nHibernate assumed it was unsaved so threw the exception being discussed (after all it didn&#8217;t want to go and attempt to create an insert in my join table using the primary key of an object that didn&#8217;t exists in the database).</p>
<p>Anyhows, changing my code to this resolved the error:<br />
Person myPerson = new Person(17765)<br />
myPerson.IsSaved = true;</p>
<p>Please note that i new a person with id 17765 already existed but i didn&#8217;t want to keep going to the DB everytime i added a person to the collection. (Going to the db and getting the person would also have prevented the error. i.e myPerson = session.Get(17765); because hnibernate knows it exist in the db when i comes to save the association)</p>
<p>Now&#8230;Another scenario where you could get this exception would be:</p>
<p>I create a new Person who does not have a record in my DB:<br />
Person myPerson = new Person(99999);<br />
myPerson.IsSaved = false;<br />
If I then add this person to the collection of my other object, lets call it &#8220;Bus&#8221;&#8230;.so&#8230;.<br />
Bus.Passengers.Add(myPerson)</p>
<p>When nhibernate attempts to save Bus if i don&#8217;t have cascade=&#8221;save-update&#8221; set in Bus.hbl.xml file then once again nhibernate will not want to insert into the join table using a myPerson.Id which does not exist in the Person database table.</p>
<p>Alternatively if i don&#8217;t want to set cascade in Bus.hbl.xml then i must explicitly save myPerson so that the record does exist before nhibernate goes to insert that join record.</p>
<p>Phew. Rambled a bit there, hope it helps someone.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aspier</title>
		<link>http://almaer.com/blog/watch-out-for-hibernate-unsaved-value/comment-page-1#comment-10854</link>
		<dc:creator>aspier</dc:creator>
		<pubDate>Sun, 27 May 2007 07:07:19 +0000</pubDate>
		<guid isPermaLink="false">http://almaer.com/blog2/watch-out-for-hibernate-unsaved-value#comment-10854</guid>
		<description>oh my god!
I also can not slove the problem?
other reasons?
help!
</description>
		<content:encoded><![CDATA[<p>oh my god!<br />
I also can not slove the problem?<br />
other reasons?<br />
help!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

