我有三张桌子和多对多的连接。在我的 JSF 应用程序中,我正在尝试添加用户。在我的组表中有三个不同的组:管理员、客户和用户。
我做了什么:
- 将数据插入 jsf-form 后,用户单击保存。
- 名为 usersController 的 ManagedBean 从 groupsController 获取组(客户组)
- usersController ManagedBean 创建新用户并将其保存到 mySQL-db。
- 问题是 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
// 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)的唯一方法是将组同时添加到组表中,但是每个用户都有自己的线路,我认为这不是目的。很多人都问过这个问题,但即使我读了这些我也想不通。
谢谢和抱歉!萨米人