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.  [ 10 posts ] 
Author Message
 Post subject: can't get one-many mapping working with one-shot delete.
PostPosted: Thu Feb 19, 2004 11:57 pm 
Newbie

Joined: Thu Feb 19, 2004 11:35 pm
Posts: 8
Hi,

I am trying to get a simple one-many mapping working in the following use-case.

---- Find Team/Players
- Open Session
- Find Team (loading the players collection (bag) )
- Close Session.


---- Update Team/Players in a later session
- Open Session
- Remove all children (players). I dont maintain the ids for the existing children and want to do bulk delete and re-insert as I only have <10 children at most.
- Add new Players (bag). using team.setPlayers(List players)
- team.update.
- close session.

Problem is that I can't get the delete-children to work. I am not sure if its possible without having ids. I also tried session.delete but that gave an exception too (stack trace below).

with session.delete() commented out, I just get the children always added and it keeps adding without deleting the existing children (players).

HIBERNATE VERSION: 2.0.2
MAPPING SNIPPET:
<bag name="Players" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="team"/>
<one-to-many class="Players"/>
</bag>

JAVA CODE:
Transaction tx = null;
Session session = Persistance.getInstance();
try {
//team.getPlayers().clear(); // DIDN'T DELETE ANY
session.delete("from players in class players where players.team = xx");
team.setPlayers(players); // set players and their team to "this"
session.update(team);
tx.commit();
} catch (HibernateException e) {
if (null != tx)
tx.rollback();
log.error("Error in update",e);
throw e;
} finally {
session.close();
}

STACK TRACE:
Error Flush during cascade is dangerous - this might occur if an object was deleted and then re-saved by cascade
net.sf.hibernate.HibernateException: Flush during cascade is dangerous - this might occur if an object was deleted and then re-saved by cascade
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2001)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:589)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1187)
at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:88)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:258)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:298)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:341)
at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2285)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2015)
at net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1560)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1372)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1332)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1457)
at updateTeam(TeamMgmt.java:72)

DATABASE: MySql 4.0.14


I will appreciate your help.

Thanks,
Tahir


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 4:31 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/117.html#A22

_________________
Emmanuel


Top
 Profile  
 
 Post subject: already did that without any luck.
PostPosted: Fri Feb 20, 2004 7:26 am 
Newbie

Joined: Thu Feb 19, 2004 11:35 pm
Posts: 8
I have already read these tips and tried all.

I am not updating the identifier of the object but only one of two other attributes and still getting the exception.

I still tried to use session.evict() just before the update but without any luck.

any other ideas??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 8:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Where players colelction come from ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re:
PostPosted: Fri Feb 20, 2004 8:47 am 
Newbie

Joined: Thu Feb 19, 2004 11:35 pm
Posts: 8
The player collection comes from the Swing GUI and passed in as the method parameter.
The team.setPlayers(players) loops through the collection (List) and assign the team as player.setTeam(thisTeam).

Only thing different here is that I dont keep the ids for players from the previously loaded data and hence just want to delete the existing players and add new players during the update.

Can you tell me how the one-shot-delete work with all-delete-orphan setting so I can try that.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 8:52 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
try to flush() after the delete.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: RE:
PostPosted: Fri Feb 20, 2004 4:33 pm 
Newbie

Joined: Thu Feb 19, 2004 11:35 pm
Posts: 8
I actually had it earlier and removed it because docs say that you should only flush once.

Anyway, I put it back. The exception is gone but now its not adding any children to the DB (I can't see any SQL for insert children from p6spy).


Can you tell me if this is NOT a standard practice to discard existing children and add new ones without worrying about comparing ids to find new/update etc?

All I want to do is delete-existing-children and save team with dependent object graph containing new children.

Please help.

Tahir


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 2:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You should not explicitly flush(), because it may be less performant (no SQL factoring of Hibernate), but you can do if you need to.

No this is not standard. Hibernate is lost, you asked it to remove them and after that to link them.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 6:44 pm 
Newbie

Joined: Thu Feb 19, 2004 11:35 pm
Posts: 8
I am lost too. I am not removing and then adding the same objects to the collection. But I want to do collection.clear() (or remove one by one) and then add the current children objects (or do a setChildren() if possible).
As the unsaved-value is zero and these are NEW object instances so hibernate should be able to insert these objects.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 7:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Show the player mapping.
I suspect you set a many-to-one to team.
If true then read that http://www.hibernate.org/155.html

_________________
Emmanuel


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