-->
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: Association Classes
PostPosted: Tue Dec 28, 2004 12:19 am 
Newbie

Joined: Tue Dec 28, 2004 12:12 am
Posts: 5
Hi I have a question about association classes.

As an example I have a class User and Group. Users can be members of many groups and Groups can have many Users. I am using an association class called UserGroup, which basically contains 2 many-to-one associations, linking a User to a Group. From the documentation this seems to be the
preferred way of modelling the many-to-many relation between User and Group. Is this assumption valid?

If so then my question is this. When I come to delete a User, I need to delete
the UserGroup entries where that user is part of. Is there an automatic way of doing this (perhaps with cascade="delete" ? ) or in my User.remove logic do I have to explicitly delete UserGroup entries prior to deleting the user?

Thanks,

Hani


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 28, 2004 5:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
1. Yes it is valied. We've barely seen a true many-to-many association in rela life applications, you usualiiy have to add some proeprties tot he association sooner or later.
2. cascade="delete" works

If you've some bucks, buy Hibernate In action, it's well explained.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 28, 2004 9:42 am 
Newbie

Joined: Tue Dec 28, 2004 12:12 am
Posts: 5
Hi Emmanuel, thanks for the reply. I have bought the ebook (missing the appendices though, since I forgot to download them when they came out).

But after re-reading Chapter 6 I am still stuck. I have no problems associating Users to Groups. The problem that I have is when I attempt to delete a User or a Group that is linked in a UserGroup association. I get
DataIntegrityViolationException which sort of makes sense. What I wanted
to know if there was a way to tell hibernate that when I delete a User I also want all UserGroup objects that reference that User to be deleted. cascade="delete" does not work, since I think it refers to the other direction.
Note that User class has no reference to UserGroup.

A solution to my problem would be to delete the UserGroup entries prior to
deleting the User. But this would complicate my code ( since plugins are
involved etc.. ). But perhaps it is the only way to do it, I just wanted to make
sure of that before I start complicating the code :)

Regards,
Hani


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 28, 2004 10:10 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
haninaguib wrote:
Note that User class has no reference to UserGroup.

You should explicit this association and mark the user.getUserGroup() as cascade="delete", otherwise, Hibernate has no (good) way to know the link.

_________________
Emmanuel


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.