Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA ManyToMany in case of users and groups?(How to avoid inserting data to groups-table, just to user and join-table)
icon9.gif  JPA ManyToMany in case of users and groups? [message #836295] Wed, 04 April 2012 06:15 Go to next message
Sami Nurmi is currently offline Sami Nurmi
Messages: 2
Registered: April 2012
Junior Member
I asked the same question in the StackOverflow with same title.
index.php/fa/7757/0/

I've got three tables and many to many connections. In my JSF-application I am trying to add users. In my groups-table there is three different groups: admin, customer and user.

What I have done:

After inserting data to jsf-form user clicks save.
ManagedBean called usersController fetches the group (customer-group) from groupsController
usersController ManagedBean creates the new user and save it to the mySQL-db.
PROBLEM is that groups_has_user-table is empty
and the code

Users:

public class Users implements Serializable {
@ManyToMany(mappedBy = "usersCollection")
private Collection<Groups> groupsCollection;

Groups:

public class Groups implements Serializable {
@JoinTable(name = "groups_has_user", joinColumns = {
@JoinColumn(name = "groups_group_id", referencedColumnName = "group_id", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "user_username", referencedColumnName = "username", nullable = false)})
@ManyToMany
private Collection<Users> usersCollection;

UsersController-managedBean

// current is type Users
current.setIsCustomer(Boolean.TRUE);


FacesContext context = FacesContext.getCurrentInstance();
GroupsController groupsC = (GroupsController) context.getApplication().evaluateExpressionGet(context, "#{groupsController}", GroupsController.class);

// Fetching group number 2, customer
Groups customerGroup = groupsC.getGroupByGroupId(new Integer(2));
List<Users> usersCollection = (List<Users>) customerGroup.getUsersCollection();
usersCollection.add(current);
//List<Groups> groupList = groupsC.getAllGroups();
customerGroup.setUsersCollection(usersCollection);
// Updating the group using GroupsController
groupsC.updateGroup(customerGroup);

//groupList.add(customerGroup);
//current.setGroupsCollection(groupList);
getFacade().create(current);

Only way how I got something to join-table (groups_has_user) is to add group at the same time to groups-table but then there is an own line for every user and I think that it's not the purpose. Many has asked that, but I couldn't figured that out even if I read those. How to avoid inserting data to groups-table in that case? I just want to have three lines of data in groups-table, now and forever, 1 admin,2 customer and 3 user?

Thanks and sorry!
Sami
  • Attachment: DB.png
    (Size: 21.71KB, Downloaded 497 times)

[Updated on: Wed, 04 April 2012 06:25]

Report message to a moderator

Re: JPA ManyToMany in case of users and groups? [message #837372 is a reply to message #836295] Thu, 05 April 2012 11:38 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1016
Registered: July 2009
Senior Member
Hello,

I'm not sure exactly what you are asking, but from the database setup and mappings, you need to uncomment the current.setGroupsCollection(groupList); code for Users to be associated to Groups. When using a relation table, there must be an entry for each and every user/group combination; A many to many means that many users can be associated to many different groups. This would allow a particular user to be in the Customer group as well as in the Admin group.
There should only be one entry for Customer in the group table unless you are inserting a new one. If that is happening, be sure you are reading the customer entity from the EntityManager when associating it to new Users. Be sure you are not creating a new Group instance and just setting the groupName field to "Customer" as that will make JPA think you want to create a new instance when it gets persisted/merged.

Best Regards,
Chris
Re: JPA ManyToMany in case of users and groups? [message #837483 is a reply to message #837372] Thu, 05 April 2012 14:24 Go to previous message
Sami Nurmi is currently offline Sami Nurmi
Messages: 2
Registered: April 2012
Junior Member
Chris Delahunt wrote on Thu, 05 April 2012 11:38
Hello,

I'm not sure exactly what you are asking, but from the database setup and mappings, you need to uncomment the current.setGroupsCollection(groupList); code for Users to be associated to Groups. When using a relation table, there must be an entry for each and every user/group combination; A many to many means that many users can be associated to many different groups. This would allow a particular user to be in the Customer group as well as in the Admin group.
There should only be one entry for Customer in the group table unless you are inserting a new one. If that is happening, be sure you are reading the customer entity from the EntityManager when associating it to new Users. Be sure you are not creating a new Group instance and just setting the groupName field to "Customer" as that will make JPA think you want to create a new instance when it gets persisted/merged.

Best Regards,
Chris


Hi and thank you very much for commenting!

You understood everything correctly. I changed the code little bit more than you asked and now it is working.

FacesContext context = FacesContext.getCurrentInstance();
            GroupsController groupsC = (GroupsController) context.getApplication().evaluateExpressionGet(context, "#{groupsController}", GroupsController.class);
            
            // Fetching group number 2, customer-group
            Groups customerGroup = groupsC.getGroupByGroupId(new Integer(2));
            System.out.println("-----------customerGroup: " +customerGroup.getGroupname());
            List<Users> usersCollection = (List<Users>) customerGroup.getUsersCollection();
            
            // Add current new user to the customer-group
            usersCollection.add(currentUser);
            customerGroup.setUsersCollection(usersCollection);
            
            // Updating the group using GroupsController, THIS INSERT USER TO USERS-table as well??
            groupsC.updateGroup(customerGroup);
            
            //Because a user is a new user, we create a group list and insert customer group into it 
            List<Groups> customersGroups = new ArrayList<Groups>();
            customersGroups.add(customerGroup);
            currentUser.setGroupsCollection(customersGroups);




I got it working Smile My code was messy and I cleaned it out and it helped. I am not so sure why updating the groups-table inserts a new user to Users-table, but it is ok to me. At first I updated groups-table and inserted user to users-table and I always got an exception that there is that pk already (username is pk). So it tried to make two inserts to users-table.

Thanks a lot!
Sami

[Updated on: Thu, 05 April 2012 17:28]

Report message to a moderator

Previous Topic:Problems with cascade delete doing update instead
Next Topic:Audit Entity
Goto Forum:
  


Current Time: Sat Aug 02 00:50:31 EDT 2014

Powered by FUDForum. Page generated in 0.02244 seconds