-->
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: pojo code for managing many-to-many relationships
PostPosted: Tue Aug 09, 2005 7:01 pm 
Newbie

Joined: Mon Jun 06, 2005 9:26 pm
Posts: 8
Hibernate version: 2.1
Mapping documents: generated by middlegen

Here's a simple association to illustrate my question:
Code:
(user)-1---*-(user_priv)-*---1-(priv)

Assume the join table has some association-specific data in it, so it needs to exist as a navigable class with two 1-many relationships. The pseudocode to manage these relationships might look like this:
Code:
UserPriv addAssociation(User user, Priv priv) {
    UserPriv assoc = new UserPriv(priv,user);
    user.getUserPrivs().add(assoc);
    priv.getUserPrivs().add(assoc);
}
void removeAssociation(UserPriv assoc) {
    assoc.getPriv().getUserPrivs().remove(assoc);
    assoc.getUser().getUserPrivs().remove(assoc);
    session.delete(assoc);
}

That last line is where my question/problem lies. Everything else could be written into the pojos, requiring no session/transaction logic to be embedded in the method. But the act of breaking an association requires object deletion, which (AFAIK) requires a call on the session -- thus making my pojo no longer "po". So how could I do this so that the pojos can remain DB-code-free?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 7:56 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
We have similar model cross over whole application. We did not use many-to-many mapping at all. What we did, we model that relation in Hibernate like

Code:
(user) has collection of (user_priv) (set)  and (user_priv) has just an attribute(priv) (many-to-one).


You need to diside what direction you will map it (depends on your B logic). In this case just put right cascade option on assiociations. For first one we used all-delete-orphans and between (user_priv) and (priv) none

_________________
Vasyl Zhabko


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 8:13 pm 
Newbie

Joined: Mon Jun 06, 2005 9:26 pm
Posts: 8
My example is also using two 1-many relationships. The need we have, which I think invalidates your offered approach, is that the table relationship needs to be bi-directional (sometimes we also want to look at all users having a priv, or want to add/remove a user to a specific a priv group). Cascading a deletion of the association from either direction is "wrong" since both ends are permanent fixtures, and only the associations are made/broken on demand.

I hope that clarifies rather than confuses... Thanks in any case.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 12:52 am 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
indgosky wrote:
My example is also using two 1-many relationships. The need we have, which I think invalidates your offered approach, is that the table relationship needs to be bi-directional (sometimes we also want to look at all users having a priv, or want to add/remove a user to a specific a priv group). Cascading a deletion of the association from either direction is "wrong" since both ends are permanent fixtures, and only the associations are made/broken on demand.

I hope that clarifies rather than confuses... Thanks in any case.


What we found with our app that cascade all-delete-orphans works just fine in almost all casses. At least for us. Usage of it does not mean you will use cascade delete. This cascade option perfectly works for purpose to save child collection. In your case assocation. Hibernane smart enough to figure out what items need to be deleted or inserted. And what you need to do just delete or add associations to collection and call session.save(user). Is`t it simple? No additional functionality that will manage your associations everything done via mappings.

_________________
Vasyl Zhabko


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.