openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Dick <michael.d.d...@gmail.com>
Subject Re: updating a many to many relationship
Date Wed, 13 Oct 2010 22:21:16 GMT
Hi Joe,

This might be a bit late, but did you try the openjpa.InverseManagerproperty?

-mike

On Sun, Oct 10, 2010 at 6:11 AM, joe <fischauto333@yahoo.de> wrote:

> Hi,
>
> The good news is i solved my problem, the bad one is its still much code
> needed to update a bi-directional ManyToMany relationship correctly. But
> it's in the nature of things.
>
>
> http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side
>
> I misunderstand JPA in some ways, what i have learned is that i have to
> update both sides of the relationship, manually.
>
> Here the working code:
>
>
> public void updateUser(User editedUser)
> {
>  User managedUser = em.find(User.class, editedUser.getId());
>
>  List<Group> groupsOfManagedUser = managedUser.getGroups();
>
>  List<Group> groupsOfEditedUser = editedUser.getGroups();
>
>  List<Group> addedGroups = determineAddedGroups(groupsOfEditedUser,
> groupsOfManagedUser);
>  List<Group> removedGroups = determineRemovedGroups(groupsOfEditedUser,
> groupsOfManagedUser);
>
>  for (Group addedGroup : addedGroups)
>  {
>    Group managedGroup = em.find(Group.class, addedGroup.getId());
>    managedGroup.getUsers().add(managedUser);
>    managedUser.getGroups().add(managedGroup);
>  }
>
>  for (Group managedGroup : removedGroups)
>  {
>    int userIndex = findUser(managedGroup.getUsers(), editedUser.getId());
>    managedGroup.getUsers().remove(userIndex);
>
>    int groupIndex = findGroup(groupsOfManagedUser, managedGroup.getId());
>    groupsOfManagedUser.remove(groupIndex);
>  }
>
>  updateOtherUserProperties(managedUser, editedUser);
> }
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message