0

编辑:我把案子改成了婚礼,但这里的情况完全一样。
我有一个这样的实体/数据库结构:组 <----> 用户 <-----> 婚礼。这意味着用户拥有婚礼,而婚礼可以有许多具有不同角色和组的用户。我正在尝试同时为注册用户和新(婚礼)用户添加婚礼。下面的代码将新婚礼插入数据库,但不是婚礼的新用户。如果我尝试添加一个新用户只是为此目的调用方法(与数据库不同的事务),我得到了一个新用户并与它结婚。之后我尝试合并 currentUser ,但 currentUser /注册用户的weddingID仍然为空。丑陋的代码,但我希望你能活下来!

用户实体:

@JoinColumn(name = "wedding_wedding_id", referencedColumnName = "wedding_id")
@ManyToOne (cascade= CascadeType.ALL)
private Wedding weddingWeddingId;

@ManyToMany(mappedBy = "usersCollection")
private Collection<Groups> groupsCollection;

婚礼实体

@OneToMany(mappedBy = "weddingWeddingId",cascade=CascadeType.ALL)
    private Collection<Users> usersCollection;

用户控制器:

public String createWeddingAndWeddingUser() {

        Users weddingUser = null;
        List<Users> usersCollection = (List<Users>) currentUser.getWeddingWeddingId().getUsersCollection();

        //Creates wedding and wedding user, one username and password for all guests
        if (currentUser.getHasGuestUsers() == Boolean.TRUE && currentUser.getWeddingWeddingId() != null) {


            // We are using Transient property
            weddingUser = currentUser.getWeddingWeddingId().getGuestUsers();

            //Fetching group for User-group. Table: Groups
            FacesContext context = FacesContext.getCurrentInstance();
            GroupsController groupsC = (GroupsController) context.getApplication().evaluateExpressionGet(context, "#{groupsController}", GroupsController.class);
            Groups userGroup = null;


            try {
                // Group 3 is group for users
                userGroup = groupsC.getGroupByGroupId(new Integer(3));
            } catch (Exception e) {
                JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("resources/Bundle").getString("PersistenceErrorOccured"));
                return null;
            }

            Collection groupCollection = new ArrayList<Groups>();
            groupCollection.add(userGroup);

            weddingUser.setGroupsCollection(groupCollection);
            // Both user must have the same wedding, first one is admin and another one is user
            weddingUser.setWeddingWeddingId(currentUser.getWeddingWeddingId());

            weddingUser.setFirstName(weddingUser.getUsername());
            weddingUser.setLastName(weddingUser.getWeddingWeddingId().getName());

            if (usersCollection == null) {
                usersCollection = new ArrayList<Users>();
            } else {
                usersCollection.add(weddingUser);
                usersCollection.add(currentUser);
            }

            currentUser.getWeddingWeddingId().setUsersCollection(usersCollection);



            try {

                // Should update the "master" user and create a new user and a wedding
                currentUser = getFacade().mergeUser(currentUser);

                JsfUtil.addSuccessMessage(ResourceBundle.getBundle("resources/Bundle").getString("WeddingAndUserCreated"));
                return "View";

            } catch (Exception e) {
                JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("resources/Bundle").getString("PersistenceErrorOccured"));
                return null;
            }


        } else {

            if (usersCollection == null) {
                usersCollection = new ArrayList<Users>();
            }
            usersCollection.add(currentUser);
            currentUser.getWeddingWeddingId().setUsersCollection(usersCollection);

            try {
              // Edit is same than MERGE                    
              getFacade().edit(currentUser);

                JsfUtil.addSuccessMessage(ResourceBundle.getBundle("resources/Bundle").getString("WeddingCreated"));
            } catch (Exception e) {
                JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("resources/Bundle").getString("PersistenceErrorOccured"));
                return null;
            }


        }

        return "View";

    }

其他块工作得很好。它只是为 currentUser 添加了一个新婚礼,一切正常。

我决定在更新派对/婚礼等时使用上层实体(用户)。例如,当用户添加婚礼时,我只需调用 MERGE USER。

你怎么看,我应该把整个案子分开吗?如果我只是先添加派对/婚礼并获得一个 ID。之后,我获取婚礼并从数据库获取自动增量 ID。然后我合并注册用户,以便它获得对婚礼的引用。之后我创建一个新用户并将婚礼ID插入其中?听起来很假,因为我认为 JPA 只是为了避免这种情况,对吗?这样就需要大量的阅读和写作……我认为在这种情况下应该是不必要的。我想一次处理所有事情,婚礼、更新当前用户(管理员)和创建婚礼用户。

4

0 回答 0