-->
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: Problem with cascade delete and many-to-one
PostPosted: Tue Apr 03, 2007 4:41 pm 
Newbie

Joined: Tue Apr 03, 2007 4:30 pm
Posts: 5
I'm trying to set up a many-to-one relationship where I have one User and multiple Sailor instances. If I delete a User, all Sailors with matching userId's should be deleted.

I don't want to have a <one-to-many> relationship in the User class, as I don't want to populate the User object with lots of Sailor objects every time I fetch the former from the database.

I'm using the mapping and DLL below. What happens is the opposite of what I want: when a Sailor is deleted, the associated User is deleted as well.

Any hints?

Hibernate version:
3.2.3

Mapping documents:
Code:
<hibernate-mapping>
   <class name="jotto.User" table="User">
    <id name="id" column="id">
      <generator class="identity" />     
    </id>

    <property name="email" />
    <property name="password" />
    <property name="name" />   
  </class>

  <class name="jotto.Sailor"
    table="Sailor">
    <id name="id" column="id">
   <generator class="identity" />
    </id>

    <property name="userId" />
    <property name="cash" />
   
    <many-to-one name="user" column="userId"
                         class="jotto.User"
                         lazy="false" not-found="ignore"
                         insert="false" update="false" cascade="delete">
    </many-to-one>
  </class>   
</hibernate-mapping>


The DDL (mySQL)

Code:
CREATE TABLE User
(
   id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
   email VARCHAR(255) NOT NULL,
   name VARCHAR(255),
   password VARCHAR(255),
   CONSTRAINT UNIQUE(email)
) ENGINE=InnoDb;

CREATE TABLE Sailor
(
   id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
   userId INT NOT NULL, 
   cash BIGINT,
   
   CONSTRAINT UNIQUE(userId),
   FOREIGN KEY (userId) REFERENCES User (id)
) ENGINE=InnoDb;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 5:17 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
you cannot cascade from a side having no relation.

Your assumption that the Sailor is loaded all the time is false. You may have a look at lazy loading in the Hibernate Reference.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 11:47 pm 
Expert
Expert

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

add bag mapping in user class.

<class name="jotto.User" table="User">
<id name="id" column="id">
<generator class="identity" />
</id>

<property name="email" />
<property name="password" />
<property name="name" />
<bag name="sailor" inverse="true" cascade="all">
<key column="userId" />
<one-to-many class="jotto.Sailor" />
</bag>
</class>


and if you try to delete user then all Sailor would delete.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 10:44 am 
Newbie

Joined: Tue Apr 03, 2007 4:30 pm
Posts: 5
Thanks! A lazy bag did the trick!


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.