-->
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.  [ 9 posts ] 
Author Message
 Post subject: cascade="all" one-to-many fails
PostPosted: Tue Nov 29, 2005 6:19 am 
I can


Top
  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 10:37 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Can you provide the code you are executing?

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: Code!
PostPosted: Wed Nov 30, 2005 5:46 am 
Code:
        [Test]
        public void CreateTest()
        {
            VersionObject ver = new VersionObject();
            VersionItem verI = new VersionItem();
            Save(verI.Version);
            Save(verI);
           
            ver.VersionItems.Add(verI);
            ver.AddCom1s(new VersionComElm1());
            ver.AddCom1s(new VersionComElm1());
            ver.AddCom1s(new VersionComElm1());
//            foreach (VersionComElm1 vc in ver.Com1s)
//            {
//                vc.Com2s.Add(new VersionComElm2());
//                vc.Com2s.Add(new VersionComElm2());
//                dataStorage.Save(vc);
//            }
            Save(ver.Version);
            Save(ver); // Exception!!!
        }


        public bool Save(Object newObject)
        {

            ISession session = aFactory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            session.Save(newObject);
            transaction.Commit(); // Exception!!!
            bool isSaved = transaction.WasCommitted;
            session.Close();

            return isSaved;
        }


Top
  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 9:34 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
You may change your Save() method to something like: http://nhibernate.sourceforge.net/NHibernateEg/NHibernateEg.Tutorial1A.html#NHibernateEg.Tutorial1A-Persistence

Can you isolate a little bit more your problem? (remove useless mapping/code)
Most of the time, this isolation proccess allows you to find a solution yourself...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 11:00 am 
Okay I have try to simplify it and surrounded the code with try/catch.

Exception massages (E1 is used so you can find the exception in the code):
SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.


MyXML:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="PersistenUnitTest.VersionObject, UnitTest" table="versionobject">
      <id name="VersionId" column= "id" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
         <generator class="assigned" />
      </id>   
    <list name="Com1s" cascade="all">
      <key column="pid" />
      <index column="posn" />
      <one-to-many class="PersistenUnitTest.VersionComElm1, UnitTest" />
    </list>
  </class>   
  <class name="PersistenUnitTest.VersionComElm1, UnitTest" table="versionComElm1">
    <id name="Id" column= "id" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="assigned" />
    </id>
    <property name="Label" column="label" type="String" />       
  </class>   
</hibernate-mapping>



MyCode:
Code:
        public bool Save(Object newObject)
        {
            ISession session = null;
            ITransaction transaction = null;
            bool isSaved = false;
            try
            {
                session = aFactory.OpenSession();
                transaction = session.BeginTransaction();
                session.Save(newObject);
                transaction.Commit(); // Exception E1 !!!
                isSaved = true;
            }
            catch // catch E1
            {
                if (transaction != null)
                    transaction.Rollback();
                throw; // throw E1!
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            return isSaved;
        }
       

        public void CreateNHibernateTest()
        {
            VersionObject ver = new VersionObject();
           
            ver.AddCom1s(new VersionComElm1());
            ver.AddCom1s(new VersionComElm1());
//            foreach (VersionComElm1 vc in ver.Com1s) // If this forach is used no exception is thrown.
//            {
//                vc.Com2s.Add(new VersionComElm2());
//                vc.Com2s.Add(new VersionComElm2());
//                Save(vc);
//            }
            Save(ver); // Exception E1 !!!
        }


Top
  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 9:49 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Can you provide the implementation of the method VersionObject.AddCom1s()?
You may also change log4net level to DEBUG and post the log...

Actually, adding VersionComElm2 items to VersionComElm1 doesn't change anything. It is saving the VersionComElm1 elements which "solves" the problem.

Note that returning isSaved is useless as Save() will either end by throwing an exception or return "true".

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 2:42 am 
It locks to me like Nhibernate updates instead of inserting!

VersionObject code:

private IList com1s;

public IList Com1s
{
get
{
if (com1s == null)
{
com1s = new ArrayList();
}

return com1s;
}
set { com1s = value; }
}

public void AddCom1s(VersionComElm1 elm1)
{
Com1s.Add(elm1);
elm1.Parent = this;
}






log4net log:

49970 [2484] ERROR NHibernate.Persister.GetSetHelperFactory (null) - Line:12, Column:3 Message:The type 'Terma.TMission.TSystem.TVersion.VersionAdapter' is defined in an assembly that is not referenced. You must add a reference to assembly'IVersion, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
49970 [2484] ERROR NHibernate.Persister.GetSetHelperFactory (null) - Line:12, Column:3 Message:The type 'Terma.TMission.TSystem.TVersion.IVersionParent' is defined in an assembly that is not referenced. You must add a reference to assembly'IVersion, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
49970 [2484] INFO NHibernate.Persister.GetSetHelperFactory (null) - Disabling reflection optimizer for class PersistenUnitTest.VersionObject
49970 [2484] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - CodeDOM compilation failedSystem.InvalidOperationException: The type 'Terma.TMission.TSystem.TVersion.VersionAdapter' is defined in an assembly that is not referenced. You must add a reference to assembly 'IVersion, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. at NHibernate.Persister.GetSetHelperFactory.Build(String code) at NHibernate.Persister.GetSetHelperFactory.Create(Type mappedClass, ISetter[] setters, IGetter[] getters)
49970 [2484] DEBUG NHibernate.Impl.SessionFactoryObjectFactory (null) - registered: e91d7e8fce7e43f6a0d1e6ab3b9d6299(unnamed)
49970 [2484] INFO NHibernate.Impl.SessionFactoryObjectFactory (null) - no name configured
49970 [2484] DEBUG NHibernate.Impl.SessionFactoryImpl (null) - Instantiated session factory
49980 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - opened session
49980 [2484] DEBUG NHibernate.Transaction.AdoTransaction (null) - begin
49980 [2484] DEBUG NHibernate.Connection.DriverConnectionProvider (null) - Obtaining IDbConnection from Driver
49990 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - generated identifier: 303ed004-aade-4634-8278-dcf4eb977ece
49990 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - saving [PersistenUnitTest.VersionObject#303ed004-aade-4634-8278-dcf4eb977ece]
50000 [2484] DEBUG NHibernate.Engine.Cascades (null) - processing cascades for:PersistenUnitTest.VersionObject
50010 [2484] DEBUG NHibernate.Engine.Cascades (null) - done processing cascadesfor: PersistenUnitTest.VersionObject
50020 [2484] DEBUG NHibernate.Impl.WrapVisitor (null) - Wrapped collection in role: PersistenUnitTest.VersionObject.Com1s
50020 [2484] DEBUG NHibernate.Engine.Cascades (null) - processing cascades for:PersistenUnitTest.VersionObject
50020 [2484] DEBUG NHibernate.Engine.Cascades (null) - cascading to collection:PersistenUnitTest.VersionObject.Com1s
50030 [2484] DEBUG NHibernate.Engine.Cascades (null) - cascading to SaveOrUpdate()
50030 [2484] DEBUG NHibernate.Engine.Cascades (null) - unsaved-value: 00000000-0000-0000-0000-000000000000
50030 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - SaveOrUpdate() previously saved instance with id: 75c0da9a-115a-477e-a186-2bfce4618e47
50030 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - updating [PersistenUnitTest.VersionComElm1#75c0da9a-115a-477e-a186-2bfce4618e47]
50030 [2484] DEBUG NHibernate.Engine.Cascades (null) - cascading to SaveOrUpdate()
50030 [2484] DEBUG NHibernate.Engine.Cascades (null) - unsaved-value: 00000000-0000-0000-0000-000000000000
50030 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - SaveOrUpdate() previously saved instance with id: 6aec4cc9-a9c2-4f10-b485-b0068b7d5f56
50030 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - updating [PersistenUnitTest.VersionComElm1#6aec4cc9-a9c2-4f10-b485-b0068b7d5f56]
50030 [2484] DEBUG NHibernate.Engine.Cascades (null) - done processing cascadesfor: PersistenUnitTest.VersionObject
50040 [2484] DEBUG NHibernate.Transaction.AdoTransaction (null) - commit
50040 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - flushing session
50040 [2484] DEBUG NHibernate.Engine.Cascades (null) - processing cascades for:PersistenUnitTest.VersionObject
50040 [2484] DEBUG NHibernate.Engine.Cascades (null) - cascading to collection:PersistenUnitTest.VersionObject.Com1s
50040 [2484] DEBUG NHibernate.Engine.Cascades (null) - cascading to SaveOrUpdate()
50040 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - SaveOrUpdate() persistent instance
50040 [2484] DEBUG NHibernate.Engine.Cascades (null) - cascading to SaveOrUpdate()
50040 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - SaveOrUpdate() persistent instance
50040 [2484] DEBUG NHibernate.Engine.Cascades (null) - done processing cascadesfor: PersistenUnitTest.VersionObject
50040 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - Flushing entities and processing referenced collections
50050 [2484] DEBUG NHibernate.Impl.AbstractVisitor (null) - Processing collection for role PersistenUnitTest.VersionObject.Com1s
50050 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - Collection found: [PersistenUnitTest.VersionObject.Com1s#303ed004-aade-4634-8278-dcf4eb977ece], was: [<unreferenced>]
50050 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - Updating entity: [PersistenUnitTest.VersionComElm1#75c0da9a-115a-477e-a186-2bfce4618e47]
50060 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - Updating entity: [PersistenUnitTest.VersionComElm1#6aec4cc9-a9c2-4f10-b485-b0068b7d5f56]
50060 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - Processing unreferencedcollections
50060 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - scheduling collection removes/(re)creates/updates
50060 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - Flushed: 1 insertions, 2 updates, 0 deletions to 3 objects
50060 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
50060 [2484] DEBUG NHibernate.Impl.Printer (null) - listing entities:
50060 [2484] DEBUG NHibernate.Impl.Printer (null) - PersistenUnitTest.VersionComElm1{Label=Com1, Id=6aec4cc9-a9c2-4f10-b485-b0068b7d5f56}
50071 [2484] DEBUG NHibernate.Impl.Printer (null) - PersistenUnitTest.VersionObject{VersionId=303ed004-aade-4634-8278-dcf4eb977ece, Com1s=[VersionComElm1#75c0da9a-115a-477e-a186-2bfce4618e47, VersionComElm1#6aec4cc9-a9c2-4f10-b485-b0068b7d5f56]}
50071 [2484] DEBUG NHibernate.Impl.Printer (null) - PersistenUnitTest.VersionComElm1{Label=Com1, Id=75c0da9a-115a-477e-a186-2bfce4618e47}
50071 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - executing flush
50071 [2484] DEBUG NHibernate.Persister.EntityPersister (null) - Inserting entity: [PersistenUnitTest.VersionObject#303ed004-aade-4634-8278-dcf4eb977ece]
50081 [2484] DEBUG NHibernate.Impl.BatcherImpl (null) - Opened new IDbCommand, open IDbCommands :1
50081 [2484] DEBUG NHibernate.Impl.BatcherImpl (null) - Building an IDbCommand object for the SqlString: INSERT INTO versionobject (id) VALUES (:id)
50081 [2484] DEBUG NHibernate.Persister.EntityPersister (null) - Dehydrating entity: [PersistenUnitTest.VersionObject#303ed004-aade-4634-8278-dcf4eb977ece]
50081 [2484] DEBUG NHibernate.Type.GuidType (null) - binding '303ed004-aade-4634-8278-dcf4eb977ece' to parameter: 0
50081 [2484] DEBUG NHibernate.SQL (null) - INSERT INTO versionobject (id) VALUES (@p0)
50151 [2484] DEBUG NHibernate.Impl.BatcherImpl (null) - Closed IDbCommand, openIDbCommands :0
50151 [2484] DEBUG NHibernate.Persister.EntityPersister (null) - Updating entity: [PersistenUnitTest.VersionComElm1#75c0da9a-115a-477e-a186-2bfce4618e47]
50151 [2484] DEBUG NHibernate.Impl.BatcherImpl (null) - Opened new IDbCommand, open IDbCommands :1
50161 [2484] DEBUG NHibernate.Impl.BatcherImpl (null) - Building an IDbCommand object for the SqlString: UPDATE versionComElm1 SET label = :label WHERE id = :id
50161 [2484] DEBUG NHibernate.Persister.EntityPersister (null) - Dehydrating entity: [PersistenUnitTest.VersionComElm1#75c0da9a-115a-477e-a186-2bfce4618e47]
50161 [2484] DEBUG NHibernate.Type.StringType (null) - binding 'Com1' to parameter: 0
50161 [2484] DEBUG NHibernate.Type.GuidType (null) - binding '75c0da9a-115a-477e-a186-2bfce4618e47' to parameter: 1
50161 [2484] DEBUG NHibernate.SQL (null) - UPDATE versionComElm1 SET label = @p0 WHERE id = @p1
50221 [2484] DEBUG NHibernate.Impl.BatcherImpl (null) - Closed IDbCommand, openIDbCommands :0
50231 [2484] ERROR NHibernate.Impl.SessionImpl (null) - could not synchronize database state with sessionNHibernate.HibernateException: SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row wasmodified or deleted by another user, or a trigger is reporting misleading row count. at NHibernate.Impl.NonBatchingBatcher.AddToBatch(Int32 expectedRowCount) at NHibernate.Persister.EntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Boolean[] includeProperty, Object oldVersion, Object obj, SqlString sqlUpdateString, ISessionImplementor session) at NHibernate.Persister.EntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Object[] oldFields, Object oldVersion, Object obj, ISessionImplementor session) at NHibernate.Impl.ScheduledUpdate.Execute() at NHibernate.Impl.SessionImpl.Execute(IExecutable executable) at NHibernate.Impl.SessionImpl.ExecuteAll(IList list) at NHibernate.Impl.SessionImpl.Execute()
50241 [2484] DEBUG NHibernate.Transaction.AdoTransaction (null) - rollback
50251 [2484] DEBUG NHibernate.Transaction.AdoTransaction (null) - running AdoTransaction.Dispose()
50251 [2484] DEBUG NHibernate.Impl.SessionImpl (null) - transaction completion


Top
  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 9:28 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Are you sure that, when created, a new entity's id has the unsaved-value ("00000000-0000-0000-0000-000000000000")?

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 1:33 pm 
You right I reinitialized my guid in the VersionObject default constructor. Sorry my mistake.
Thank you for exceptionally god support!
:lol:


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