我知道如果表只有外键列,如何将行插入/更新到多对多关系的中间表中:
@Entity
@Table(name = "role")
public class Role {
@Id
private String role_code;
private String role_lib;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "role_menu" , joinColumns = {@JoinColumn(name = "role_code")} , inverseJoinColumns = {@JoinColumn(name = "menu_id")} )
private Set<Menu> menus = new HashSet<Menu>();
public Role() {
super();
}
public Role(String role_lib) {
super();
this.role_lib = role_lib;
}
public Role(String role_lib, Set<Menu> menus) {
super();
this.role_lib = role_lib;
this.menus = menus;
}
public String getRole_code() {
return role_code;
}
public void setRole_code(String role_code) {
this.role_code = role_code;
}
public String getRole_lib() {
return role_lib;
}
public void setRole_lib(String role_lib) {
this.role_lib = role_lib;
}
public Set<Menu> getMenus() {
return menus;
}
public void setMenus(Set<Menu> menus) {
this.menus = menus;
}
}
@Entity
@Table(name = "menu")
public class Menu {
@Id
@SequenceGenerator(name="s_menu", sequenceName="s_menu", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s_menu")
private int menu_id;
private Integer gmnu_code;
private String menu_lib;
private Integer menu_ordre;
private Integer menu_visible;
private Integer menu_deleted;
private Integer menu_bouton;
private Integer menu_parent;
private String menu_controlleur;
private String menu_navigation;
private Integer menu_flag_trace;
private String menu_trace;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "menus")
private Set<Role> roles = new HashSet<Role>();
@Transient
private int depth;
@Transient
private Environment env;
public Menu() {
super();
}
public Menu(Integer gmnu_code, String menu_lib, Integer menu_ordre, Integer menu_visible, Integer menu_deleted, Integer menu_bouton, Integer menu_parent, String menu_controlleur, String menu_navigation, Integer menu_flag_trace, String menu_trace) {
super();
this.gmnu_code = gmnu_code;
this.menu_lib = menu_lib;
this.menu_ordre = menu_ordre;
this.menu_visible = menu_visible;
this.menu_deleted = menu_deleted;
this.menu_bouton = menu_bouton;
this.menu_parent = menu_parent;
this.menu_controlleur = menu_controlleur;
this.menu_navigation = menu_navigation;
this.menu_flag_trace = menu_flag_trace;
this.menu_trace = menu_trace;
}
public Menu(Integer gmnu_code, String menu_lib, Integer menu_ordre, Integer menu_visible, Integer menu_deleted, Integer menu_bouton, Integer menu_parent, String menu_controlleur, String menu_navigation, Integer menu_flag_trace, String menu_trace, Set<Role> roles) {
super();
this.gmnu_code = gmnu_code;
this.menu_lib = menu_lib;
this.menu_ordre = menu_ordre;
this.menu_visible = menu_visible;
this.menu_deleted = menu_deleted;
this.menu_bouton = menu_bouton;
this.menu_parent = menu_parent;
this.menu_controlleur = menu_controlleur;
this.menu_navigation = menu_navigation;
this.menu_flag_trace = menu_flag_trace;
this.menu_trace = menu_trace;
this.roles = roles;
}
public int getMenu_id() {
return menu_id;
}
public void setMenu_id(int menu_id) {
this.menu_id = menu_id;
}
public Integer getGmnu_code() {
return gmnu_code;
}
public void setGmnu_code(Integer gmnu_code) {
this.gmnu_code = gmnu_code;
}
public String getMenu_lib() {
return menu_lib;
}
public void setMenu_lib(String menu_lib) {
this.menu_lib = menu_lib;
}
public Integer getMenu_ordre() {
return menu_ordre;
}
public void setMenu_ordre(Integer menu_ordre) {
this.menu_ordre = menu_ordre;
}
public Integer getMenu_visible() {
return menu_visible;
}
public void setMenu_visible(Integer menu_visible) {
this.menu_visible = menu_visible;
}
public Integer getMenu_deleted() {
return menu_deleted;
}
public void setMenu_deleted(Integer menu_deleted) {
this.menu_deleted = menu_deleted;
}
public Integer getMenu_bouton() {
return menu_bouton;
}
public void setMenu_bouton(Integer menu_bouton) {
this.menu_bouton = menu_bouton;
}
public Integer getMenu_parent() {
return menu_parent;
}
public void setMenu_parent(Integer menu_parent) {
this.menu_parent = menu_parent;
}
public String getMenu_controlleur() {
return menu_controlleur;
}
public void setMenu_controlleur(String menu_controlleur) {
this.menu_controlleur = menu_controlleur;
}
public String getMenu_navigation() {
if (this.env != null)
return env.getProperty(menu_navigation);
return menu_navigation;
}
public void setMenu_navigation(String menu_navigation) {
this.menu_navigation = menu_navigation;
}
public String getMenu_trace() {
if (this.env != null)
return env.getProperty(menu_trace);
return menu_trace;
}
public void setMenu_trace(String menu_trace) {
this.menu_trace = menu_trace;
}
public Integer getMenu_flag_trace() {
return menu_flag_trace;
}
public void setMenu_flag_trace(Integer menu_flag_trace) {
this.menu_flag_trace = menu_flag_trace;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public void setEnv(Environment env) {
this.env = env;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
}
@Controller
@RequestMapping("/role")
public class AdminRole {
@Autowired
private MenuDAO menuDao;
@Autowired
private RoleDAO roleDao;
...
@RequestMapping(value = "/menu", method = RequestMethod.POST)
public ModelAndView attribuerMenus(HttpServletRequest request, HttpSession session, @RequestParam Map<String, String> params) { // inserting rows into the intermediate table
String role_code = "";
Set<Menu> menus = new HashSet<Menu>();
for (Map.Entry<String, String> param : params.entrySet()) { // input name : param.getKey() , input value : param.getValue()
if (param.getKey().equals(new String("role_code")))
role_code = param.getValue();
else
menus.add(menuDao.get(Integer.parseInt(param.getValue())));
}
roleDao.insertRoleMenus(roleDao.get(role_code), menus);
return new ModelAndView("redirect:/role/");
}
}
public class RoleDAOImpl implements RoleDAO {
@Autowired
private SessionFactory sessionFactory;
public RoleDAOImpl() {
super();
}
public RoleDAOImpl(SessionFactory sessionFactory) {
super();
this.sessionFactory = sessionFactory;
}
...
@Override
@Transactional
public Role get(String role_code) {
return (Role) sessionFactory.getCurrentSession().get(Role.class, role_code);
}
@Override
@Transactional
public void insertRoleMenus(Role role, Set<Menu> menuSet) {
role.setMenus(menuSet);
sessionFactory.getCurrentSession().merge(role);
}
}
现在,如果中间表“role_menu”有一个额外的列,比如说some_column varchar2(100)
,我该如何将数据插入/更新到这个列“some_column”?
答案 0 :(得分:1)
为它创建实体:
@Entity
RoleMenu
@ManyToOne Menu
@ManyToOne Role
String some_column
在现有课程中使用一对多:
@OneToMany
RoleMenu