JPA ManyToMany in case of users and groups?

Go To StackoverFlow.com

1

Same question here, but it didn't help me

DB: users-table, groups and group_has_user

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:

  1. After inserting data to jsf-form user clicks save.
  2. ManagedBean called usersController fetches the group (customer-group) from groupsController
  3. usersController ManagedBean creates the new user and save it to the mySQL-db.
  4. 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);

//BalusC!! Why this is not working but the one below is?
//FacesContext facesContext = FacesContext.getCurrentInstance();
//HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
//GroupsController groupsC = (GroupsController)session.getAttribute("groupsController");

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.

Thanks and sorry! Sami

2012-04-04 00:16
by Sami


1

I got it working. 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 the pk already (username is the pk). So it made two inserts to users-table, but now it's working perfectly. No more lines to groups-table just to joint-table and users-table.

// Add current new user to the customer-group
            usersCollection.add(currentUser);
            customerGroup.setUsersCollection(usersCollection);

            // Updating the group using GroupsController, THIS INSERTS USER TO USERS-table as well!!
            groupsC.updateGroup(customerGroup);
2012-04-05 18:28
by Sami
Ads