-->
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.  [ 5 posts ] 
Author Message
 Post subject: Parent-Child cascade problem, cannot disable cascade?
PostPosted: Mon Mar 08, 2004 12:16 am 
Newbie

Joined: Sun Mar 07, 2004 11:56 pm
Posts: 2
Hibernate version: 2.0.3

I have a User class and a Permission class, both are entities. When I update a User's Set of Permissions in the GUI, I need to delete the old Permissions set and insert the new one. I'm using synthetic ids for both.
I gave up on finding how to use cascade for this scenario. I spent 2 days reading resources (user reference, forums, etc), I could never make it work. I tried delete/insert Permissions explicitely but cascades still got in the way.
So I decided to set cascade="none" in the Set mapping of the parent, User. However I get the following exception:

1) testUpdatePermissions(com.yellowasp.usad.dao.UserDAOTest)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.flush(SessionImpl.java:2004)
at com.yellowasp.usad.dao.UserDAO.updatePermissions(UserDAO.java:136)
at com.yellowasp.usad.dao.UserDAOTest.testUpdatePermissions(UserDAOTest.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.yellowasp.usad.dao.UserDAOTest.main(UserDAOTest.java:33)

Why do I get a cascade-related error when I set it to none? My mappings:

<hibernate-mapping>

<class name="com.yellowasp.usad.User" table="T_User" >
<id name="id" >
<generator class="native" />
</id>
<property name="firstName" length="20" />
<property name="lastName" length="20" />
<property name="userName" length="20" unique="true" />
<property name="password" length="20"/>
<property name="status" type="com.yellowasp.usad.UserStatus" />

<set name="permissions"
inverse="true"
cascade="none" >
<key column="owner" />
<one-to-many class="com.yellowasp.usad.UserPerm" />
</set>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="com.yellowasp.usad.UserPerm" table="T_UserPerm" >
<id name="id" >
<generator class="native" />
</id>
<many-to-one
name="owner" cascade="none"
class="com.yellowasp.usad.User"
not-null="true"
/>
<many-to-one
name="zone"
class="com.yellowasp.usad.UserZone"
/>
<property name="level" type="com.yellowasp.usad.PermLevel" />
</class>

<class name="com.yellowasp.usad.UserGroupPerm" table="T_UserGroupPerm" >
<id name="id" >
<generator class="native" />
</id>
<many-to-one
name="owner"
class="com.yellowasp.usad.UserGroup"
/>
<many-to-one
name="zone"
class="com.yellowasp.usad.UserZone"
/>
<property name="level" type="com.yellowasp.usad.PermLevel" />
</class>
</hibernate-mapping>

In frustration I added cascade="none" in the Child mapping as well, which of course didn't make any difference.

My code:
// load Hibernate collection type
user = (User) session.load(User.class, user.getId());

// delete children
java.util.Iterator it = user.getPermissions().iterator();
while ( it.hasNext() ) {
Permission p = (Permission) it.next();
p.setOwner(null);
session.delete(p);
}
// update parent
user.getPermissions().clear();

session.flush();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 5:50 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Just a random suggestion from a random forum member: try removing the cascade attribute altogether. I am not sure what cascade="none" does but I do know that leaving the attribute off does not cascade!

Cheers
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 8:24 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You're not showing the right code.
Permission is not mapped.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 8:25 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
RobJellinghaus wrote:
Just a random suggestion from a random forum member: try removing the cascade attribute altogether. I am not sure what cascade="none" does but I do know that leaving the attribute off does not cascade!

cascade="none" is the default value.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 9:00 pm 
Newbie

Joined: Sun Mar 07, 2004 11:56 pm
Posts: 2
emmanuel wrote:
You're not showing the right code.
Permission is not mapped.

Sorry, here it is. UserPerm extends Permission without adding anything, it's just a marker to distinguish between user permissions and user group permissions (UserGroupPerm). I'm not showing UserGroup for clarity.

<class name="com.yellowasp.usad.UserPerm" table="T_UserPerm" >
<id name="id" >
<generator class="native" />
</id>
<many-to-one
name="owner"
class="com.yellowasp.usad.User"
not-null="true"
cascade="none"
/>
<many-to-one
name="zone"
class="com.yellowasp.usad.UserZone"
not-null="true"
/>
<property name="level" type="com.yellowasp.usad.PermLevel" />
</class>


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