openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Wes Wannemacher <>
Subject Re: Conceptual question on ManyToMany Relation
Date Tue, 06 Oct 2009 16:05:18 GMT
I have an app where I am modelling users/groups, but instead of
leaving it up to the JPA implementation, I created my own junction
table via an intermediary entity. That way, I just have a one-to-many
mapping from users to the UserGroup entity and a one-to-many from
groups to UserGroup. To make it easier, I added @Transient methods to
get a users' groups and the groups' users. This way I can closely
control the behavior.

You definitely want to use Lazy fetch groups where ever possible in
this sort of setup because loading one user could conceivably load all
of the data from all of the tables.

On Tue, Oct 6, 2009 at 11:57 AM, Srinivasan Krishnamoorthy
<> wrote:
> Hi,
>  I have difficulty in understanding the JPA implementation of ManyToMany
> relation, conceptually.
> Let me try to put forward the question with an example.
> I have a many to many relation between users and groups.
> My use case is, I want to add a user to a group. Ideally, with SQL all that
> I would be doing is create a new link in a link table. But with JPA, I see
> the following steps to be done.
> 1. Get a group object, and add the user to the 'User Set'. (If I have 1000
> users I need to fetch them all).
> 2. Add the group to 'Group Set' of User Object. (Here too, I need to fetch
> all the groups, say 100).
> 3. JPA will do a 'merge' to find just the extra link and save it.
> My question is, why should I do so much to just add a link? Is there an
> easier way to do this.. or is my understanding wrong.. Can you please share
> your thoughts on this?
> Also, I am afraid, what will happen if both sides of ManyToMany has huge
> data (theoretically).
> (PS: With OneToMany I can work around this issue by modeling the system to
> always work from Many side, but dont have an idea for ManyToMany)
> Best Regards,
> Srinivasan Krishnamoorthy.
> --
> View this message in context:
> Sent from the OpenJPA Users mailing list archive at

Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

View raw message