Hi,
I am getting the following error after I add a painting then try to delete it again.
a different object with the same identifier value was already associated with the session: 105, of entity: HughGRice.Portal.Core.Models.Painting
I can delete paintings that have not been created in the same session but get the above error when they are.
My delete code looks as follows (I am using repositories):
public void Delete(int paintingId)
{
Painting painting = paintingRepository.Get(paintingId);
paintingRepository.BeginTransaction();
paintingRepository.Delete(painting);
paintingRepository.CommitTransaction();
}
My create code looks like:
public void Create(Painting paintingToCreate)
{
paintingToCreate.Gallery = galleryRepository.Get(paintingToCreate.Gallery.Id);
PaintingRepository.BeginTransaction();
paintingRepository.Create(paintingToCreate);
PaintingRepository.CommitTransaction();
}
I am unsure of why this may be happening and would appreciate any pointers in the right direction.
Thanks
Brendan
Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="HughGRice.Portal.Core.Models.PaintingType, HughGRice.Portal.Core" table="PaintingType">
<id name="Id" type="Int32" unsaved-value="null">
<column name="Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_PaintingType"/>
<generator class="identity" />
</id>
<property name="Name" type="String">
<column name="Name" length="50" sql-type="nvarchar" not-null="false"/>
</property>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="HughGRice.Portal.Core.Models.Gallery, HughGRice.Portal.Core" table="Gallery">
<id name="Id" type="Int32" unsaved-value="null">
<column name="Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Gallery"/>
<generator class="identity" />
</id>
<property name="Name" type="String">
<column name="Name" length="150" sql-type="nvarchar" not-null="false"/>
</property>
<property name="Telephone" type="String">
<column name="Telephone" length="50" sql-type="nvarchar" not-null="false"/>
</property>
<property name="URL" type="String">
<column name="URL" length="50" sql-type="nvarchar" not-null="false"/>
</property>
<property name="Email" type="String">
<column name="Email" length="50" sql-type="nvarchar" not-null="false"/>
</property>
<many-to-one name="Address" class="HughGRice.Portal.Core.Models.Address, HughGRice.Portal.Core" column="AddressId" unique="true" cascade="all" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="HughGRice.Portal.Core.Models.Painting, HughGRice.Portal.Core" table="Painting">
<id name="Id" type="Int32" unsaved-value="0">
<column name="Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Painting"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="Name" length="100" sql-type="nvarchar" not-null="true"/>
</property>
<property name="Description" type="String">
<column name="Description" length="200" sql-type="nvarchar" not-null="false"/>
</property>
<property name="Media" type="String">
<column name="Media" length="100" sql-type="nvarchar" not-null="false"/>
</property>
<property name="Size" type="String">
<column name="Size" length="20" sql-type="nvarchar" not-null="false"/>
</property>
<property name="Price" type="Decimal">
<column name="Price" length="8" sql-type="money" not-null="false"/>
</property>
<property name="Sold" type="Boolean">
<column name="Sold" length="1" sql-type="bit" not-null="false"/>
</property>
<property name="Photo" type="String">
<column name="Photo" length="50" sql-type="nvarchar" not-null="false"/>
</property>
<many-to-one name="Gallery" class="HughGRice.Portal.Core.Models.Gallery, HughGRice.Portal.Core">
<column name="GalleryId" length="4" sql-type="int" not-null="false"/>
</many-to-one>
<many-to-one name="PaintingType" class="HughGRice.Portal.Core.Models.PaintingType, HughGRice.Portal.Core">
<column name="PaintingTypeId" length="4" sql-type="int" not-null="false"/>
</many-to-one>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public void Delete(T entity)
{
if (!this.IsOpen)
{
throw new InvalidOperationException("Repository must be open before an entity can be deleted.");
}
if (_transaction == null)
{
throw new InvalidOperationException("Deletes must be done within a transaction.");
}
_session.Delete(entity);
}
public void Create(T entity)
{
if (!this.IsOpen)
{
throw new InvalidOperationException("Repository must be open before an entity can be saved.");
}
if (_transaction == null)
{
throw new InvalidOperationException("Saves must be done within a transaction.");
}
_session.Save(entity);
}
Full stack trace of any exception that occurs:
NHibernate.DLL!NHibernate.Engine.StatefulPersistenceContext.CheckUniqueness(NHibernate.Engine.EntityKey key = {EntityKey[HughGRice.Portal.Core.Models.Painting#105]}, object obj = {HughGRice.Portal.Core.Models.Painting}) Line 678 C#
NHibernate.DLL!NHibernate.Event.Default.DefaultDeleteEventListener.OnDelete(NHibernate.Event.DeleteEvent event = {NHibernate.Event.DeleteEvent}, Iesi.Collections.ISet transientEntities = {NHibernate.Util.IdentitySet}) Line 69 + 0xd bytes C#
NHibernate.DLL!NHibernate.Event.Default.DefaultDeleteEventListener.OnDelete(NHibernate.Event.DeleteEvent event = {NHibernate.Event.DeleteEvent}) Line 30 + 0x1d bytes C#
NHibernate.DLL!NHibernate.Impl.SessionImpl.FireDelete(NHibernate.Event.DeleteEvent event = {NHibernate.Event.DeleteEvent}) Line 1874 + 0x1a bytes C#
NHibernate.DLL!NHibernate.Impl.SessionImpl.Delete(object obj = {HughGRice.Portal.Core.Models.Painting}) Line 327 + 0x1f bytes C#
HughGRice.Portal.Core.DLL!HughGRice.Portal.Core.Repositories.NHibernateRepository<HughGRice.Portal.Core.Models.Painting>.Delete(HughGRice.Portal.Core.Models.Painting entity = {HughGRice.Portal.Core.Models.Painting}) Line 549 + 0xc bytes C#
HughGRice.Portal.Core.DLL!HughGRice.Portal.Core.Services.PaintingService.Delete(int paintingId = 105) Line 81 + 0xb bytes C#
HughGRice.Portal.Web.DLL!HughGRice.Portal.Web.Controllers.PaintingController.DeletePainting(int paintingIdToDelete = 105) Line 255 + 0xb bytes C#
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
2008-06-03 23:43:42,308 DEBUG SELECT user0_.Id as Id29_2_, user0_.Name as Name29_2_, user0_.Password as Password29_2_, user0_.Email as Email29_2_, group1_.Id as Id30_0_, group1_.Name as Name30_0_, applicatio2_.Id as Id34_1_, applicatio2_.Name as Name34_1_ FROM [User] user0_ left outer join [Group] group1_ on user0_.Id=group1_.Id left outer join ApplicationScope applicatio2_ on user0_.Id=applicatio2_.Id WHERE user0_.Id=@p0; @p0 = '1'
2008-06-03 23:43:42,310 DEBUG SELECT accessrigh0_.GroupId as GroupId__1_, accessrigh0_.AccessRightId as AccessRi2_1_, accessrigh1_.Id as Id32_0_, accessrigh1_.Name as Name32_0_ FROM GroupAccessRights accessrigh0_ left outer join AccessRight accessrigh1_ on accessrigh0_.AccessRightId=accessrigh1_.Id WHERE accessrigh0_.GroupId=@p0; @p0 = '1'
2008-06-03 23:43:46,088 DEBUG SELECT painting0_.Id as Id1_3_, painting0_.Name as Name1_3_, painting0_.Description as Descript3_1_3_, painting0_.Media as Media1_3_, painting0_.Size as Size1_3_, painting0_.Price as Price1_3_, painting0_.Sold as Sold1_3_, painting0_.Photo as Photo1_3_, painting0_.GalleryId as GalleryId1_3_, painting0_.PaintingTypeId as Paintin10_1_3_, gallery1_.Id as Id8_0_, gallery1_.Name as Name8_0_, gallery1_.Telephone as Telephone8_0_, gallery1_.URL as URL8_0_, gallery1_.Email as Email8_0_, gallery1_.AddressId as AddressId8_0_, address2_.Id as Id6_1_, address2_.AddressLine1 as AddressL2_6_1_, address2_.AddressLine2 as AddressL3_6_1_, address2_.Country as Country6_1_, address2_.PostalCode as PostalCode6_1_, paintingty3_.Id as Id0_2_, paintingty3_.Name as Name0_2_ FROM Painting painting0_ left outer join Gallery gallery1_ on painting0_.GalleryId=gallery1_.Id left outer join Address address2_ on gallery1_.AddressId=address2_.Id left outer join PaintingType paintingty3_ on painting0_.PaintingTypeId=paintingty3_.Id WHERE painting0_.Id=@p0; @p0 = '105'
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html