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.