-->
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.  [ 6 posts ] 
Author Message
 Post subject: Object already associated with session on delete
PostPosted: Tue Jun 03, 2008 6:59 pm 
Newbie

Joined: Wed Feb 20, 2008 8:08 pm
Posts: 10
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 2:24 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
Hi,
in your delete method you try to load the same object again:

Code:
Painting painting = paintingRepository.Get(paintingId);

That's not possible, because nhibernate can have one object only once in a session. Try to evict the object first:

Code:
Session.Evict(yourObject);


Or you can overload your delete method to take the whole object as parameter:

Code:
Delete(YourObjectType obj)
{
    paintingRepository.BeginTransaction();
    paintingRepository.Delete(obj);
    paintingRepository.CommitTransaction();
}


Top
 Profile  
 
 Post subject: problem
PostPosted: Tue Jul 29, 2008 2:29 am 
Newbie

Joined: Tue May 13, 2008 5:22 am
Posts: 7
public void deleteDetails(Long detailOid, List<Detail> detailList)
throws GenericBusinessException {
Detail model = (Detail)getHibernateTemplate().get(Detail.class, detailOid);
try {
getHibernateTemplate().deleteAll(detailList);

} catch (DataAccessException e) {
log.error("DataAccessException", e);
throw new GenericBusinessException(e);
} finally {
log.debug("finished deleting Detail");
}
this throws exception called a different object with the same identifier value was already associated with the session
on deleteall.

I am passing the detail list to delete from other function .I dont understand why i am getting this exception.


Top
 Profile  
 
 Post subject: Re: problem
PostPosted: Tue Jul 29, 2008 3:39 am 
Newbie

Joined: Tue May 13, 2008 5:22 am
Posts: 7
Hi,

public void deleteDetails(Long detailOid, List<Detail> detailList)
throws GenericBusinessException {
Detail model = (Detail)getHibernateTemplate().get(Detail.class, detailOid);
try {
getHibernateTemplate().deleteAll(detailList);

} catch (DataAccessException e) {
log.error("DataAccessException", e);
throw new GenericBusinessException(e);
} finally {
log.debug("finished deleting Detail");
}
this throws exception called a different object with the same identifier value was already associated with the session
on deleteall.

I am passing the detail list to delete from other function .I dont understand why i am getting this exception.Can anybody tell me y i am getting this exception

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 9:46 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You probably have a problem with object identities. I assume that Equals() on those instance returns false even if the id is the same. Have a look here:

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/04/identity-field-equality-and-hash-code.aspx

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 7:32 am 
Newbie

Joined: Wed Sep 27, 2006 2:54 pm
Posts: 15
i found another solution for this exception:

Use cascade = "none" on those relations that cause this exception

P.S : Specially when you are defining both direction relations ( for example a bag with one-to-many in parent object , and a many-to-one to child object ) use "none" in many-to-one relation in child solves the problem.


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