-->
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.  [ 2 posts ] 
Author Message
 Post subject: Automatically remove from collections on delete
PostPosted: Tue Aug 16, 2005 10:57 pm 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
I delete an object:

Session s = this.factory.openSession();
Transaction tx = s.beginTransaction();
s.delete( s.get( Y.class, new Integer( yId ) ) );
tx.commit();
s.close();

And then I get something on the other side of a many to many relationship:

Session s = factory.openSession();
Query q = s.createQuery( "FROM X" );
List l = q.list();
this.x = ( X ) l.get( 0 );
this.x.getYs();
s.close();

And get the stack trace below. I check the database and the Y is indeed gone from the database but the mapping table Y<->X still points to that ID.

If I'm going to delete an object, do I have to manually go through and remove it from any and all collections it might be a part of?

I did read the part on transitive persistence:

http://www.hibernate.org/hib_docs/v3/re ... transitive

But that doesn't look like it will help me.

Thanks!


Hibernate version:

3.1 beta 2


Full stack trace of any exception that occurs:

org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
[Y#7]
at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundExcep
tion.java:27)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEven
tListener.java:121)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultL
oadEventListener.java:170)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEv
entListener.java:82)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:668)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:270)
at org.hibernate.type.EntityType.resolve(EntityType.java:305)
at org.hibernate.type.EntityType.nullSafeGet(EntityType.java:216)
at org.hibernate.persister.collection.AbstractCollectionPersister.readEl
ement(AbstractCollectionPersister.java:623)
at org.hibernate.collection.PersistentSet.readFrom(PersistentSet.java:23
7)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:942)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:619)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:563)
at org.hibernate.loader.Loader.doQuery(Loader.java:666)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Lo
ader.java:221)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1787)
at org.hibernate.loader.collection.CollectionLoader.initialize(Collectio
nLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initia
lize(AbstractCollectionPersister.java:500)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onIn

Name and version of the database you are using:

MySQL 5


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:28 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
This works, but it depends on which side of the many-to-many inverse is set to 'false'.

In your case the Y mapping shoud have
Code:
<set
            name="xs"
            table="x_y_table"
            lazy="true"
            inverse="false"
            cascade="none"
            sort="unsorted"
        >


and then ofcourse X

Code:
<set
            name="ys"
            table="x_y_table"
            lazy="true"
            inverse="true"
            cascade="none"
            sort="unsorted"
        >


But then if you delete X first, the same as before would happen to Y. I think then you should programaticaly remove the association.

I think there was a good page on hibernate.org about this, but I just can't find it right now.


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