-->
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.  [ 4 posts ] 
Author Message
 Post subject: TransientObjectException while saving a mapped collection
PostPosted: Thu Jan 11, 2007 4:21 pm 
Newbie

Joined: Tue Jan 09, 2007 7:38 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 1.2.0 Beta 3 (one from a few days ago)

Mapping documents:
ProjectInclude:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateCETest" assembly="NHibernateCETest">
   <class name="ProjectInclude" table="ProjectIncludes" lazy="false">
      <id name="IncludeID" column="ID" unsaved-value="0" access="field">
         <generator class="native" />
      </id>
      <map name="ParmArr" cascade="all" table="ParameterIndex" access="field" lazy="false">
         <key column="ProjectID" />
         <index-many-to-many column="ParameterID" class="ProjectParameter" />
         <element column="keyCol" type="String"/>
      </map>
   </class>
</hibernate-mapping>


Project Parameter:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateCETest" assembly="NHibernateCETest">
   <class name="ProjectParameter" table="ProjectParameters" lazy="false">
      <id name="parmID" column="ParameterID" unsaved-value="0" access="field">
         <generator class="native" />
      </id>
      <property name="name" column="Name" access="field"/>
      <property name="val" column="Val" access="field"/>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Exception causing:
Code:
ProjectInclude project = MakeProject();
ises.SaveOrUpdate(project);
ises.Flush();

Exception free:
Code:
ProjectInclude project = MakeProject();
ises.SaveOrUpdate(project);
foreach (ProjectParameter p in project.ParmArr.Keys)
   ises.SaveOrUpdate(p);
ises.Flush();


ProjectInclude.cs
Code:
   public class ProjectInclude
   {
      public IDictionary ParmArr=new Hashtable();
      public int IncludeID = 0;

      public String this[String name]
      {
         set
         {
            ProjectParameter t=new ProjectParameter(name, value);
            ParmArr[t] = name;
         }
      }

      public int Count
      {
         get { return ParmArr.Count; }
      }

      public override bool Equals(object obj)
      {
         ProjectInclude pi = obj as ProjectInclude;
         return pi.ParmArr.Count == ParmArr.Count;
      }

      public override int GetHashCode()
      {
         return ParmArr.Count;
      }
   }

Full stack trace of any exception that occurs:
Code:
   at NHibernate.Impl.SessionImpl.ThrowTransientObjectException(Object obj) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3748
   at NHibernate.Impl.SessionImpl.GetEntityIdentifierIfNotUnsaved(Object obj) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3737
   at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) in c:\net\nhibernate\nhibernate\src\NHibernate\Type\EntityType.cs:line 80
   at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand cmd, Object value, Int32 index, ISessionImplementor session) in c:\net\nhibernate\nhibernate\src\NHibernate\Type\ManyToOneType.cs:line 69
   at NHibernate.Persister.Collection.AbstractCollectionPersister.WriteIndex(IDbCommand st, Object idx, Int32 i, ISessionImplementor session) in c:\net\nhibernate\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 603
   at NHibernate.Persister.Collection.AbstractCollectionPersister.Recreate(IPersistentCollection collection, Object id, ISessionImplementor session) in c:\net\nhibernate\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 936
   at NHibernate.Impl.ScheduledCollectionRecreate.Execute() in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\ScheduledCollectionRecreate.cs:line 32
   at NHibernate.Impl.SessionImpl.Execute(IExecutable executable) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3264
   at NHibernate.Impl.SessionImpl.ExecuteAll(IList list) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3243
   at NHibernate.Impl.SessionImpl.Execute() in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3181
   at NHibernate.Impl.SessionImpl.Flush() in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3015
   at NHibernateCETest.CETestFixture.SaveThrowsNoException() in C:\Documents and Settings\mattmoss\Desktop\ESL Lookup\NHibernateCETest\CETestFixture.cs:line 45


Name and version of the database you are using: MSSQL 2005

The generated SQL (show_sql=true): Not Relevant

Debug level Hibernate log excerpt:
Code:
2007-01-11 14:15:19,471 [TestRunnerThread] DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - unsaved-value: 0
2007-01-11 14:15:19,502 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - SaveOrUpdate() unsaved instance
2007-01-11 14:15:19,502 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - saving [NHibernateCETest.ProjectInclude#<null>]
2007-01-11 14:15:19,517 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - executing insertions
2007-01-11 14:15:19,517 [TestRunnerThread] DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - processing cascades for: NHibernateCETest.ProjectInclude
2007-01-11 14:15:19,517 [TestRunnerThread] DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - done processing cascades for: NHibernateCETest.ProjectInclude
2007-01-11 14:15:19,533 [TestRunnerThread] DEBUG NHibernate.Impl.WrapVisitor [(null)] <(null)> - Wrapped collection in role: NHibernateCETest.ProjectInclude.ParmArr
2007-01-11 14:15:19,533 [TestRunnerThread] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Inserting entity: NHibernateCETest.ProjectInclude (native id)
2007-01-11 14:15:19,549 [TestRunnerThread] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Opened new IDbCommand, open IDbCommands: 1
2007-01-11 14:15:19,549 [TestRunnerThread] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Building an IDbCommand object for the SqlString: INSERT INTO ProjectIncludes DEFAULT VALUES; select SCOPE_IDENTITY()
2007-01-11 14:15:19,549 [TestRunnerThread] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Dehydrating entity: [NHibernateCETest.ProjectInclude#<null>]
2007-01-11 14:15:19,549 [TestRunnerThread] DEBUG NHibernate.Connection.DriverConnectionProvider [(null)] <(null)> - Obtaining IDbConnection from Driver
2007-01-11 14:15:19,705 [TestRunnerThread] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Opened IDataReader, open IDataReaders: 1
2007-01-11 14:15:19,736 [TestRunnerThread] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Natively generated identity: 1
2007-01-11 14:15:19,736 [TestRunnerThread] DEBUG NHibernate.Driver.NHybridDataReader [(null)] <(null)> - running NHybridDataReader.Dispose()
2007-01-11 14:15:19,736 [TestRunnerThread] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Closed IDataReader, open IDataReaders :0
2007-01-11 14:15:19,736 [TestRunnerThread] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Closed IDbCommand, open IDbCommands: 0
2007-01-11 14:15:19,736 [TestRunnerThread] DEBUG NHibernate.Impl.ConnectionManager [(null)] <(null)> - aggressively releasing database connection
2007-01-11 14:15:19,736 [TestRunnerThread] DEBUG NHibernate.Connection.ConnectionProvider [(null)] <(null)> - Closing connection
2007-01-11 14:15:19,752 [TestRunnerThread] DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - processing cascades for: NHibernateCETest.ProjectInclude
2007-01-11 14:15:19,752 [TestRunnerThread] DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - done processing cascades for: NHibernateCETest.ProjectInclude
2007-01-11 14:15:19,752 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - flushing session
2007-01-11 14:15:19,752 [TestRunnerThread] DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - processing cascades for: NHibernateCETest.ProjectInclude
2007-01-11 14:15:19,752 [TestRunnerThread] DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - done processing cascades for: NHibernateCETest.ProjectInclude
2007-01-11 14:15:19,752 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - Flushing entities and processing referenced collections
2007-01-11 14:15:19,767 [TestRunnerThread] DEBUG NHibernate.Impl.AbstractVisitor [(null)] <(null)> - Processing collection for role NHibernateCETest.ProjectInclude.ParmArr
2007-01-11 14:15:19,767 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - Collection found: [NHibernateCETest.ProjectInclude.ParmArr#1], was: [<unreferenced>]
2007-01-11 14:15:19,767 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - Processing unreferenced collections
2007-01-11 14:15:19,767 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - scheduling collection removes/(re)creates/updates
2007-01-11 14:15:19,767 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2007-01-11 14:15:19,767 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
2007-01-11 14:15:19,767 [TestRunnerThread] DEBUG NHibernate.Impl.Printer [(null)] <(null)> - listing entities:
2007-01-11 14:15:19,783 [TestRunnerThread] DEBUG NHibernate.Impl.Printer [(null)] <(null)> - NHibernateCETest.ProjectInclude{IncludeID=1, ParmArr=[BLDG4:1, BLDG3]}
2007-01-11 14:15:19,783 [TestRunnerThread] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - executing flush
2007-01-11 14:15:19,783 [TestRunnerThread] DEBUG NHibernate.Impl.ConnectionManager [(null)] <(null)> - registering flush begin
2007-01-11 14:15:19,783 [TestRunnerThread] DEBUG NHibernate.Persister.Collection.ICollectionPersister [(null)] <(null)> - Inserting collection: [NHibernateCETest.ProjectInclude.ParmArr#1]
2007-01-11 14:15:19,799 [TestRunnerThread] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Opened new IDbCommand, open IDbCommands: 1
2007-01-11 14:15:19,799 [TestRunnerThread] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Building an IDbCommand object for the SqlString: INSERT INTO ParameterIndex (ProjectID, ParameterID, keyCol) VALUES (?, ?, ?)
2007-01-11 14:15:19,799 [TestRunnerThread] DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - unsaved-value: 0
2007-01-11 14:15:19,799 [TestRunnerThread] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Closed IDbCommand, open IDbCommands: 0
2007-01-11 14:15:19,799 [TestRunnerThread] DEBUG NHibernate.Impl.ConnectionManager [(null)] <(null)> - skipping aggressive-release due to flush cycle
2007-01-11 14:15:19,799 [TestRunnerThread] ERROR NHibernate.Impl.SessionImpl [(null)] <(null)> - could not synchronize database state with session
NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: NHibernateCETest.ProjectParameter
   at NHibernate.Impl.SessionImpl.ThrowTransientObjectException(Object obj) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3748
   at NHibernate.Impl.SessionImpl.GetEntityIdentifierIfNotUnsaved(Object obj) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3737
   at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) in c:\net\nhibernate\nhibernate\src\NHibernate\Type\EntityType.cs:line 80
   at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand cmd, Object value, Int32 index, ISessionImplementor session) in c:\net\nhibernate\nhibernate\src\NHibernate\Type\ManyToOneType.cs:line 69
   at NHibernate.Persister.Collection.AbstractCollectionPersister.WriteIndex(IDbCommand st, Object idx, Int32 i, ISessionImplementor session) in c:\net\nhibernate\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 603
   at NHibernate.Persister.Collection.AbstractCollectionPersister.Recreate(IPersistentCollection collection, Object id, ISessionImplementor session) in c:\net\nhibernate\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 936
   at NHibernate.Impl.ScheduledCollectionRecreate.Execute() in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\ScheduledCollectionRecreate.cs:line 32
   at NHibernate.Impl.SessionImpl.Execute(IExecutable executable) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3264
   at NHibernate.Impl.SessionImpl.ExecuteAll(IList list) in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3243
   at NHibernate.Impl.SessionImpl.Execute() in c:\net\nhibernate\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3175
2007-01-11 14:15:19,846 [TestRunnerThread] DEBUG NHibernate.Impl.ConnectionManager [(null)] <(null)> - registering flush end
2007-01-11 14:15:19,846 [TestRunnerThread] DEBUG NHibernate.Impl.ConnectionManager [(null)] <(null)> - aggressively releasing database connection


Problems with Session and transaction handling?

I'm unsure of how much of that will actually be needed, but, none the less.

The ProjectInclude file has an IDictionairy that maps an instance of ProjectParameter to a String. When I attempt to save (and then flush) a normal ProjectInclude instance, it throws the exception above. If I manually go through and save the keys before flushing the session, it works. Is there anyway to have NHibernate take care of saving the keys? (I though setting cascade="all" would have, though it seems I was mistaken...). Any ideas? Anything obviously wrong with the mapping file?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 11, 2007 8:28 pm 
Newbie

Joined: Wed Jan 10, 2007 5:15 pm
Posts: 7
I had this problem before under the following scenario.

Lets say you have a Country object which holds reference to several City objects. If you attempt to persist Country before you have persisted all of its children (i.e. City objects), then you obtain the transient exception.
(Well, I am not 100% right but the scenario is close enough to most others)

So, check your "cascading level" attributes. It should probably be "save-update" or something stronger than what you have now.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 12, 2007 12:49 am 
Newbie

Joined: Tue Jan 09, 2007 7:38 pm
Posts: 5
Thank you for your input.

I've tried it with save-update to the same effect. It seems to occur whenever an entity is set as the key for a map.

After some in depth browsing arround the forums I've found a few bug reports from about 4-6 months ago, but the content wasn't useful. Can anyone confirm whether or not it's a bug?

As far as a work arround, I *think* implementing the ILifeCycle in the ProjectInclude might work. Then on update and saving save the keys (I don't have the exact docs in front of me, though I think that is right).

On a somewhat different note (I've only been at this for a few days so forgive the newbishness) I thought save-update was included in 'all' as far as cascading levels are concerned...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 5:58 am 
Newbie

Joined: Tue Feb 06, 2007 5:46 am
Posts: 10
I had this problem and adding cascade as the above poster suggested along with removing unsaved-value="0" fixed it for me.

_________________
Josh Coady
http://jlcoady.net/


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