3

I generated Entities and Facades using netbeans. Below is an example that works/updates:

I have 2 tables that are joined by a join table where it works:

User

PK: userID

Category

PK: categoryID

User_Category

Composite PK: userID | categoryID (no other columns)

Method to update join table, taking care of relationships on both sides:

    //set up user Categories
    List<Category> categoryList = new ArrayList<Category>();

    //get all the current categories for the current user
    categoryList.addAll(user.getCategoryList());

    //clear out user for that type (to reset)
    for(Category c : categoryList){
        tempUserList.clear();
        tempUserList = c.getUserList();
        tempUserList.remove(user);
        c.setUserList(tempUserList);
        categoryFacade.edit(c);
    }

    categoryList.clear();
    //where selectedCategoryListString is a list of category names
    for(String s : selectedCategoryListString){
        categoryList.add(categoryFacade.findByCategoryName(s));
    }

    for(Category c : categoryList){
        tempUserList.clear();
        tempUserList = c.getUserList();
        tempUserList.add(user);
        c.setUserList(tempUserList);
        categoryFacade.edit(c);
    }
    user.setCategoryList(categoryList);
    userFacade.edit(user);

The above works, it takes care of both sides of the relationship. Where I'm stuck is below. A 'skill' does not have the method '.getUserList()' because of the extra 'years' column. Instead, it has a '.getUserSkillList()'. I am lost as to how I should be updating the join table like I did above. We can assume that the 'years' column gets set to "" (blank) every time.

Below: problematic tables/relationship:

User

userID

Skill

skillID

UserSkill

userID | skillID | years

What's important is: User has a userID. Skill has a skillID, UserSkill has a composite primary key made up of userID and skillID and the other column; years.

4

2 回答 2

0

如果您使用的是 hibernate,则只需在 class 的Cascade.UPDATEoverUserSkill属性上添加一个即可解决该问题User

但是,由于您没有使用休眠,您可以手动完成。更新 aUser时,检索UserSkills与它相关的那些并更新它们。

如果您对此有任何具体问题,请告诉我以更新我的答案

于 2013-04-06T05:56:55.713 回答
0

如果没有看到您的实体类,很难确定,但您可能正在寻找类似的东西:

@ManyToMany(cascade = CascadeType.MERGE, ...)

级联类型

于 2013-04-06T09:01:07.787 回答