-->
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.  [ 2 posts ] 
Author Message
 Post subject: Best way to manage collection / linked table ?
PostPosted: Sun Aug 21, 2011 2:30 pm 
Newbie

Joined: Sun Aug 21, 2011 2:19 pm
Posts: 2
Hi,

I'm not sure if this has been discussed before, couldn't find anything in the search (well, I could find a lot, but nothing matching exactly...).

I'm looking at how I can manage a many-to-many relationship in a less complex way than I'm currently doing.

I've got a spring/hibernate small app to manage two simple entities, User and Subscription.

The way I've got this currently modeled is with a collection of subscriptions on the User object:

Code:
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(
   name="USER_SUBS",
   joinColumns = @JoinColumn(name="USER_ID"),
   inverseJoinColumns = @JoinColumn(name="SUBSCRIPTION_ID")
)
public Collection<Subscription> getUserSubscriptions() {
...
}


At the service level, I have methods to add/remove a subscription from a user. The front end is passing only the subscription ID, so my steps are at the moment:

1. Fetch the Subscription object using a subscriptionService
2. In my userDAO, call:

Code:
public void addSubscription(User user, Subscription sub) {
   user.getSubscriptions().add(sub);
   getHibernateTemplate().saveOrUpdate(user);
}


And something similar to remove.

Now, I'm wondering if there's something else I could do, since this seems to be doing a lot of stuff just to do the equivalent of a "DELETE FROM USER_SUBS WHERE USER_ID = ? AND SUB_ID = ?"

* I have to load up the proper Subscription object since I have only the ID
* I resave the whole user just to persist the change to their subs collection

What trick am I missing? How can I manager that link table to add/remove those links in a lighter manner? Anything I could call on getHibernateTemplate() in the DAO?

Am I wrong in thinking there is an overhead? Is second level caching allowing these steps to be just as fast as something more "targeted"?


Top
 Profile  
 
 Post subject: Re: Best way to manage collection / linked table ?
PostPosted: Tue Aug 23, 2011 5:06 am 
Newbie

Joined: Sun Aug 21, 2011 2:19 pm
Posts: 2
Guess it's not a very good question :)

I've changed the approach a little bit at the moment, where I'm now creating a different object called UserProfile that contains the collection of subscriptions, so that I don't update the user all the time, I supposed it's a little bit better but still not ideal.


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