Hibernate version: 1.2 cr1
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Department,Test" table="Portrait">
<id name="Id" column="id">
<generator class="identity"></generator>
</id>
<version name="Version"/>
<!-- ... other props goes here ...--!>
<property name="CreatedDate" not-null="true"></property>
<property name="ModifiedDate" not-null="true"></property>
<property name="CreatedBy" length="50" not null="true"></property>
<property name="ModifiedBy" length="50" not-null="true"></property>
<list name="Personell" inverse="false" table="Personell" cascade="save-update">
<key column="PortraitId"></key>
<index column="ItemID"></index>
<many-to-many class="ClassPerson,Test" column="Item">
</many-to-many>
</list>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="ClassPerson,Test" table="Person">
<id name="Id" column="id">
<generator class="identity"></generator>
</id>
<version name="Version" />
<property name="FirstName" length="50"></property>
<property name="LastName" not-null="true" length="50"></property>
<property name="CreatedDate" not-null="true"></property>
<property name="ModifiedDate" not-null="true"></property>
<property name="CreatedBy" length="50" not-null="true"></property>
<property name="ModifiedBy" length="50" not-null="true">
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Departmen dep = new Department();
ClassPerson pers1 = new ClassPerson();
ClassPerson pers2 = new ClassPerson();
// Fill data to "dep"
...
// Fill data to "pers1" & "pers2"
dep.Personell.Add(pers1);
dep.Personell.Add(pers2);
ISession session = this.GetSession(new BaseInterceptor(GetUser(),DateTime.Now));
ITransaction transaction = session.Transaction;
transaction.Begin();
session.save(dep); //This throws an exception
.....
Full stack trace of any exception that occurs:Code:
"a different object with the same identifier value was already associated with the session: 0, of class: ClassPerson"
" at NHibernate.Impl.SessionImpl.CheckUniqueness(EntityKey key, Object obj)\r\n at NHibernate.Impl.SessionImpl.DoUpdateMutable(Object obj, Object id, IEntityPersister persister)
at NHibernate.Impl.SessionImpl.DoUpdate(Object obj, Object id, IEntityPersister persister)
at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)
at NHibernate.Engine.Cascades.CascadingAction.ActionSaveUpdateClass.Cascade(ISessionImplementor session, Object child, Object anything)
at NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, Object child, IType type, CascadingAction action, CascadeStyle style, CascadePoint cascadeTo, Object anything)
at NHibernate.Engine.Cascades.CascadeCollection(CascadingAction action, CascadeStyle style, CollectionType collectionType, IType elemType, Object child, CascadePoint cascadeVia, ISessionImplementor session, Object anything)
at NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, Object child, IType type, CascadingAction action, CascadeStyle style, CascadePoint cascadeTo, Object anything)
at NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, IEntityPersister persister, Object parent, CascadingAction action, CascadePoint cascadeTo, Object anything)
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, EntityKey key, IEntityPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at
...
...
...
Name and version of the database you are using:
SQL Server 2005
So, what have I done wrong.
The first ClassPerson "pers1" is inserted ok into the database (trace from SQL server) and when Hibernate tries to insert "pers2" the exception occurs.
One thing that confuses me is that Id in ClassPerson is 0,,, and there is no place in my code, that initiates Id to 0. I had expected it to be null.
Anyone that can help me?
//regards
//lassse