如果是用户和组,JPA ManyToMany吗? [英] JPA ManyToMany in case of users and groups?

查看:59
本文介绍了如果是用户和组,JPA ManyToMany吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相同问题在这里,但对我没有帮助

我有三个表和许多对许多连接.在我的JSF应用程序中,我试图添加用户.在我的分组表中,有三个不同的分组:admin,customer和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.

我做了什么:

  1. 将数据插入jsf表单后,用户单击保存.
  2. 被称为usersController的ManagedBean从groupsController获取组(客户组)
  3. usersController ManagedBean创建新用户并将其保存到mySQL-db.
  4. 问题是groups_has_user-table为空

和代码

用户:

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

组:

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

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);

唯一让我加入联接表(groups_has_user)的方法是同时将组添加到groups-table,但是每个用户都有一行,我认为这不是目的.许多人都问过这个问题,但是即使我读了这些,我也想不通.

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.

谢谢,抱歉! 萨米人

推荐答案

我知道了.我的代码杂乱无章,我将其清理干净,并有所帮助.我不确定为什么更新groups-table会将新用户插入Users-table,但这对我来说还可以.最初,我更新了groups-table并将用户插入到users-table中,但我总是遇到一个例外,即已经有pk(用户名是pk).因此,它在用户表中插入了两个插入物,但是现在可以正常工作了.不再有到组表的行,而只是到联合表和用户表的行.

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);

这篇关于如果是用户和组,JPA ManyToMany吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆