Hibernate version: 1.2
Problem in Detail:
In the current scenario I have two tables:
Authors;
Work;
with many-to-many relationship to each other in Nhibernate. There is also a third table author_work which is used by hibernate to map many-to-may relationship. The read operation is performed successfully but we are still unable to create the Author like following;
Author author = new Author();
author.Works = new List<Work>();
author.Works.Add(new Work());
author.Works.Add(new Work());
/* code to persist Author*/
gw.Channel.CreateBusinessDocument(author);
I'm getting exception that "transient object 'Work' need to be saved first". I'm also providing the hbm.xml files of Author and Work.
The code is working fine if implemented in Java & Hibernate but the issue is with Nhibernate & .Net
I will be really thankful to all of you for your replies.
Mapping documents:
Work.hbm.xml:
==========
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="Calibr.BusinessDocuments.BOs.Work, BusinessDocuments" table="Work">
<id name="Id" type="Int32">
<column name="id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Work"/>
<generator class="identity" />
</id>
<!--id name="Id" column="id">
<generator class="assigned"/>
</id-->
<property name="Text" type="String">
<column name="text" length="50" sql-type="varchar" not-null="false"/>
</property>
<bag name="Authors" table="Author_Work" inverse="true">
<key column="work_id"/>
<many-to-many class="Calibr.BusinessDocuments.BOs.Author, BusinessDocuments" column="author_id"/>
</bag>
</class>
</hibernate-mapping>
Author.hbm.xml
===========
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="Calibr.BusinessDocuments.BOs.Author, BusinessDocuments" table="Author">
<id name="Id" type="Int32">
<column name="id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Authour"/>
<generator class="identity" />
</id>
<property name="Name" type="String">
<column name="name" length="25" sql-type="varchar" not-null="false"/>
</property>
<bag name="Works" table="Author_Work" >
<key column="author_id"/>
<many-to-many class="Calibr.BusinessDocuments.BOs.Work, BusinessDocuments" column="work_id"/>
</bag>
</class>
</hibernate-mapping>
Debug Info Full stack trace of any exception that occurs + generated SQL:
21:09:53.125 [13] DEBUG NHibernate.Impl.SessionFactoryImpl - Instantiated session factory
21:09:53.531 [13] DEBUG NHibernate.Impl.SessionImpl - opened session
21:09:54.421 [13] DEBUG NHibernate.Transaction.AdoTransaction - begin
21:09:54.468 [13] DEBUG NHibernate.Impl.SessionImpl - saving [Calibr.BusinessDocuments.BOs.Author#<null>]
21:09:54.468 [13] DEBUG NHibernate.Impl.SessionImpl - executing insertions
21:09:54.500 [13] DEBUG NHibernate.Impl.WrapVisitor - Wrapped collection in role: Calibr.BusinessDocuments.BOs.Author.Works
21:09:54.515 [13] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Inserting entity: Calibr.BusinessDocuments.BOs.Author (native id)
21:09:54.562 [13] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
21:09:54.562 [13] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO Author (name) VALUES (?); select SCOPE_IDENTITY()
21:09:54.562 [13] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Dehydrating entity: [Calibr.BusinessDocuments.BOs.Author#<null>]
21:09:54.562 [13] DEBUG NHibernate.Type.StringType - binding 'Waqas' to parameter: 0
21:09:54.578 [13] DEBUG NHibernate.SQL - INSERT INTO Author (name) VALUES (@p0); select SCOPE_IDENTITY(); @p0 = 'Waqas'
21:09:54.671 [13] DEBUG NHibernate.Impl.BatcherImpl - Opened IDataReader, open IDataReaders: 1
21:09:54.703 [13] DEBUG NHibernate.Type.Int32Type - returning '10' as column:
21:09:54.703 [13] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Natively generated identity: 10
21:09:54.718 [13] DEBUG NHibernate.Driver.NHybridDataReader - running NHybridDataReader.Dispose()
21:09:54.718 [13] DEBUG NHibernate.Impl.BatcherImpl - Closed IDataReader, open IDataReaders :0
21:09:54.718 [13] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 0
21:09:54.734 [13] DEBUG NHibernate.Transaction.AdoTransaction - commit
21:09:54.734 [13] DEBUG NHibernate.Impl.SessionImpl - flushing session
21:09:54.750 [13] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
21:09:54.765 [13] DEBUG NHibernate.Impl.AbstractVisitor - Processing collection for role Calibr.BusinessDocuments.BOs.Author.Works
21:09:54.765 [13] DEBUG NHibernate.Impl.SessionImpl - Collection found: [Calibr.BusinessDocuments.BOs.Author.Works#10], was: [<unreferenced>]
21:09:54.765 [13] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
21:09:54.765 [13] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
21:09:54.765 [13] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
21:09:54.765 [13] DEBUG NHibernate.Impl.SessionImpl - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
21:09:54.781 [13] DEBUG NHibernate.Impl.Printer - listing entities:
21:09:54.781 [13] DEBUG NHibernate.Impl.Printer - Calibr.BusinessDocuments.BOs.Author{Name=Waqas, Works=[Work#6, Work#0], Id=10}
21:09:54.781 [13] DEBUG NHibernate.Impl.SessionImpl - executing flush
21:09:54.781 [13] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
21:09:54.796 [13] DEBUG NHibernate.Persister.Collection.ICollectionPersister - Inserting collection: [Calibr.BusinessDocuments.BOs.Author.Works#10]
21:09:54.796 [13] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
21:09:54.796 [13] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO Author_Work (author_id, work_id) VALUES (?, ?)
21:09:54.796 [13] DEBUG NHibernate.Type.Int32Type - binding '10' to parameter: 0
21:09:54.796 [13] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
21:09:54.796 [13] DEBUG NHibernate.Type.Int32Type - binding '6' to parameter: 1
21:09:54.796 [13] DEBUG NHibernate.SQL - INSERT INTO Author_Work (author_id, work_id) VALUES (@p0, @p1); @p0 = '10', @p1 = '6'
21:09:54.812 [13] DEBUG NHibernate.Type.Int32Type - binding '10' to parameter: 0
21:09:54.812 [13] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
21:09:54.828 [13] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 0
21:09:54.843 [13] ERROR NHibernate.Impl.SessionImpl - could not synchronize database state with session
NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Calibr.BusinessDocuments.BOs.Work
at NHibernate.Impl.SessionImpl.ThrowTransientObjectException(Object obj)
at NHibernate.Impl.SessionImpl.GetEntityIdentifierIfNotUnsaved(Object obj)
at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session)
at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session)
at NHibernate.Persister.Collection.AbstractCollectionPersister.WriteElement(IDbCommand st, Object elt, Int32 i, ISessionImplementor session)
at NHibernate.Persister.Collection.AbstractCollectionPersister.Recreate(IPersistentCollection collection, Object id, ISessionImplementor session)
at NHibernate.Impl.ScheduledCollectionRecreate.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
Name and version of the database you are using: SQL Server 2005
CREATE TABLE [dbo].[Author](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_Authour] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Work](
[id] [int] NOT NULL,
[text] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_Work] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Author_Work](
[author_id] [int] NOT NULL,
[work_id] [int] NOT NULL,
CONSTRAINT [PK_Author_Work] PRIMARY KEY CLUSTERED
(
[author_id] ASC,
[work_id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
|