同样的问题在这里,但这对我没有帮助
![DB: users-table, groups and group_has_user](https://i.stack.imgur.com/hKMM3.png)
我有三个表和多对多连接。在我的 JSF 应用程序中,我尝试添加用户。在我的组表中,有三个不同的组:管理员、客户和用户。
我做了什么:
- 将数据插入 jsf 表单后,用户单击“保存”。
- 名为 usersController 的 ManagedBean 从 groupsController 中获取组(客户组)
- usersController ManagedBean 创建新用户并将其保存到 mySQL-db。
- 问题是 groups_has_user-table 为空
和代码
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;
用户控制器管理Bean
// 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)的唯一方法是同时将组添加到组表中,但是每个用户都有自己的行,我认为这不是目的。很多人问过这个问题,但即使我读了这些,我也无法弄清楚。
谢谢并抱歉!
萨米人