-->
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: Cascade delete overspill?
PostPosted: Thu Mar 22, 2007 7:16 am 
Newbie

Joined: Thu Mar 22, 2007 6:50 am
Posts: 6
Hi all,

I'm new to Hibernate and I have a question about cascade delete...

In my application I'm using user/role relationship in the following way:

Code:
CREATE TABLE pb_user (
   id INTEGER NOT NULL PRIMARY KEY,
   username VARCHAR(128) NOT NULL UNIQUE,
   password VARCHAR(128) NOT NULL,
   email_address VARCHAR(128) NOT NULL UNIQUE,
   enabled BOOLEAN NOT NULL,
   creation_date TIMESTAMP NOT NULL,
   last_visit TIMESTAMP NOT NULL
);

CREATE TABLE pb_authority (
   id INTEGER NOT NULL PRIMARY KEY,
   authority VARCHAR(128) NOT NULL,
   description VARCHAR(1024)
);

CREATE TABLE pb_granted_authority (
   user_id INTEGER NOT NULL,
   authority_id INTEGER NOT NULL
);

ALTER TABLE pb_granted_authority ADD CONSTRAINT c2ad45de FOREIGN KEY (user_id) REFERENCES pb_user(id);
ALTER TABLE pb_granted_authority ADD CONSTRAINT d4ffa12e FOREIGN KEY (authority_id) REFERENCES pb_authority(id);


This is mapped to Hibernate as follows:

Code:
<hibernate-mapping auto-import="true" default-lazy="false">
  <class name="pb.user.User" table="pb_user">
    <id name="id" type="long" column="id" unsaved-value="0">
      <generator class="increment" />
    </id>
    <property name="username" type="string" column="username" length="128" not-null="true" unique="true" />
    <property name="password" type="string" column="password" length="128" not-null="true" unique="true" />
    <property name="emailAddress" type="string" column="email_address" length="128" not-null="true" unique="true" />
    <property name="enabled" type="boolean" column="enabled" not-null="true" />
    <property name="creationDate" type="timestamp" column="creation_date" not-null="true" />
    <property name="lastVisit" type="timestamp" column="last_visit" not-null="true" />
    <set name="grantedAuthorities" table="pb_granted_authority" cascade="all">
      <key column="user_id" />
      <many-to-many column="authority_id" class="pb.user.Authority" />
    </set>
  </class>
  <class name="pb.user.Authority" table="pb_authority">
    <id name="id" type="long" column="id" unsaved-value="0">
      <generator class="increment" />
    </id>
    <property name="authority" type="string" column="authority" length="128" />
    <property name="description" type="string" column="description" length="1024" />
  </class>
</hibernate-mapping>


This works fine when creating users (the user is created in pb_user and also any entries in the pb_granted_authority table).

However, when I delete a user, the row in the pb_authority reference table is also deleted. This is not what I want. pb_authority should be a reference table managed outside of any application code. It lists the available authorities and should be only referenced.

What I want is that, when I delete a user, the corresponding granted authorities are also removed from the pb_granted_authorities table, but the reference authorities in pb_authority should NOT be removed.

How can I do that? Is it simply removing the foreign key constraint to pb_authority from pb_granted_authority?

Many thanks,
James


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 22, 2007 7:50 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
hi jamesdavide,

I am not able to understand from where you have maped pb_authority with cascade in use.But if you want to refer any entity use cascade="none" in mapping .

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 22, 2007 8:01 am 
Newbie

Joined: Thu Mar 22, 2007 6:50 am
Posts: 6
Users can have 0-n authorities granted. The granted authorities are stored in pb_granted_authority (user_id, authority_id). The authorities themselves are normalized and stored in a table pb_authoritiy.

I think the row is deleted unintentionally as follows:

1) When a User is deleted the corresponding row is deleted from pb_user
2) All granted authorities are also deleted from pb_granted_authorities (that is why I need some form of cascade delete on the grantedAuthorities Set that is a property of User (so this is required behaviour)
3) But because pb_granted_authorities are referring to normalized authorities stored in pb_authorities the referred authority is also deleted (this is not what I want as this is normalized data that should only be referenced from pb_granted_authority)

I want the cascade to stop at the pb_granted_authority and not spill over to pb_authorities.

Hope this clarifies my issue.

Many thanks,
James


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 22, 2007 7:34 pm 
Newbie

Joined: Thu Mar 22, 2007 6:50 am
Posts: 6
cascade="none" seems to work
I thought I needed the cascade to also delete the granted authorities, but I don't.

Thanks,
james


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 12:12 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi jamesdavide,

Plz.. don't forget to give credit if some is there that helps you

_________________
Dharmendra Pandey


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.