-->
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.  [ 4 posts ] 
Author Message
 Post subject: Cascade all-delete-orphan not deleting
PostPosted: Sat Mar 21, 2009 12:56 pm 
Newbie

Joined: Tue May 31, 2005 7:50 am
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.6.ga

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.trovare.dal.dto.User" schema="PUBLIC" table="APP_USER">
  <id name="id" type="long">
   <column name="usr_id"/>
   <generator class="native">
    <param name="sequence">app_user_usr_id_seq</param>
   </generator>
  </id>
  <property generated="never" lazy="false" name="firstName" type="string">
   <column length="50" name="USR_FIRST_NAME"/>
  </property>
  <property generated="never" lazy="false" name="lastName" type="string">
   <column length="50" name="USR_LAST_NAME"/>
  </property>
  <property generated="never" lazy="false" name="userName" type="string">
   <column length="50" name="USR_USER_NAME" unique="true"/>
  </property>
  <property generated="never" lazy="false" name="emailAddress" type="string">
   <column length="75" name="USR_EMAIL_ADDRESS"/>
  </property>
  <property generated="never" lazy="false" name="password" type="string">
   <column length="50" name="USR_PASSWORD"/>
  </property>
  <property generated="never" lazy="false" name="created" type="date">
   <column name="USR_CREATED"/>
  </property>
  <set cascade="save-update" lazy="false" inverse="true"
   name="userIndexLinks" sort="unsorted">
   <key>
    <column name="UIL_USR_ID" not-null="true"/>
   </key>
   <one-to-many class="com.trovare.dal.dto.UserIndexLink"/>
  </set>
  <set lazy="false" inverse="true" name="userIndexGroupLinks" sort="unsorted">
   <key>
    <column name="UIGL_USR_ID" not-null="true"/>
   </key>
   <one-to-many class="com.trovare.dal.dto.UserIndexGroupLink"/>
  </set>
  <set lazy="false"  inverse="true" name="userRoleLinks" sort="unsorted" cascade="all-delete-orphan">
   <key>
    <column name="URL_USR_ID" not-null="true"/>
   </key>
   <one-to-many class="com.trovare.dal.dto.UserRoleLink"/>
  </set>
  <set lazy="false" inverse="true" name="preferences" sort="unsorted">
   <key>
    <column name="PREF_ID" not-null="true" unique="true"/>
   </key>
   <one-to-many class="com.trovare.dal.dto.Preference"/>
  </set>
</class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
Code:
   public User save(User user) {
      
      User newUser = null;
      
      try {
         HibernateUtil.beginTransaction();
         IUserDAO userDAO = getDaoFactory().getUserDAO();
         newUser = userDAO.save(user);
         HibernateUtil.commitTransaction();
      } catch (InfrastructureException e) {
         logger.error(e.getLocalizedMessage());
         HibernateUtil.rollbackTransaction();
      } catch (HibernateException e) {
         logger.error(e.getLocalizedMessage());
         logger.error(e.getMessage());
         HibernateUtil.rollbackTransaction();
      } finally {
         HibernateUtil.closeSession();
      }
      
      return newUser;
   }




Full stack trace of any exception that occurs: N/A

Name and version of the database you are using: PostgreSQL 8.3

The generated SQL (show_sql=true):
Hibernate: update PUBLIC.APP_USER set USR_FIRST_NAME=?, USR_LAST_NAME=?, USR_USER_NAME=?, USR_EMAIL_ADDRESS=?, USR_PASSWORD=?, USR_CREATED=? where usr_id=?
Hibernate: select this_.ROL_ID as ROL1_12_0_, this_.ROL_NAME as ROL2_12_0_, this_.ROL_DESCRIPTION as ROL3_12_0_ from PUBLIC.APP_ROLE this_
Hibernate: select this_.ROL_ID as ROL1_12_0_, this_.ROL_NAME as ROL2_12_0_, this_.ROL_DESCRIPTION as ROL3_12_0_ from PUBLIC.APP_ROLE this_



I have a database used to hold user credentials and as part of this I have a user table and a role table and an assoicated join table user_role_link. The user.hbm.xml files maps the user_role_link as a set and has a cascade vlaue of all-delete-orphan, but when I programatically remove a link from the UserRoleLink set in the User object the corresponding row in the user_role_link table is not deleted.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 21, 2009 4:10 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
mapping looks ok. Can you show us more? in particular, inside userdao.save?
Also, are you re-attaching the user object to the session where it was previously loaded? Might want to double check your equals/hash methods on userlink class too.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 22, 2009 8:26 am 
Newbie

Joined: Tue May 31, 2005 7:50 am
Posts: 7
The UserDAO inherits from GenericHibernateDAO and the save method looks like this:

Code:
   public T saveOrUpdate(T entity) {
      getSession().saveOrUpdate(entity);
      return entity;
   }


The hashCode and equals methods are as you'd expect. I am not re-attaching however, so perhaps this is the problem. I thought the call to saveOrUpdate would handle this.

Thanks for you reply.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 22, 2009 10:16 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
If you're passing in a user object to this method :

public User save(User user) {

And this user object was loaded in a different hibernate session, I'm pretty certain calling saveOrUpdate on the entity won't do anything. This is because the object is no longer in the session cache.

Is your user object your passing in an object created in a prior connection/session?

I'm not that familiar with reattaching objects, as I prefer to reload fresh from hibernate, and apply changes from a DTO object, but it will probably work if you choose session.merge(user)
Hope this helps,
James


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