我根据用户角色使用spring和hibernate创建动态菜单。它工作正常,菜单生成。但是菜单和子菜单之间的关系不能正常工作。我只想要分配了角色的子菜单。但它包含所有子菜单。
我的POJO如下所示。
Menu.java
package com.pmc.domain;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Fetch;
@Entity
@Table(name="menu", catalog="test")
public class Menu implements Serializable{
@Id
@Column(name = "menu_item_id")
private Integer menuId;
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="MENU_ROLES",
joinColumns={@JoinColumn(name="menu_item_id")},
inverseJoinColumns={@JoinColumn(name="user_role_id")})
private List<UserRole> userRoles;
@Column(name = "label", length = 100)
private String label;
@Column(name = "link", length = 100)
private String link;
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name = "parent")
private Menu parent;
@OneToMany(mappedBy="parent",fetch = FetchType.EAGER)
private List<Menu> submenus;
public Integer getMenuId() {
return menuId;
}
public void setMenuId(Integer menuId) {
this.menuId = menuId;
}
public List<UserRole> getUserRoles() {
return userRoles;
}
public void setUserRoles(List<UserRole> userRoles) {
this.userRoles = userRoles;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Menu getParent() {
return parent;
}
public void setParent(Menu parent) {
this.parent = parent;
}
public List<Menu> getSubmenus() {
return submenus;
}
public void setSubmenus(List<Menu> submenus) {
this.submenus = submenus;
}
}
UserRole.java
package com.pmc.domain;
import static javax.persistence.GenerationType.IDENTITY;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table (name = "roles", catalog = "test")
public class UserRole {
private Integer userRoleId;
private Set<User> users;
private String role;
private List<Menu> menus;
public UserRole() {
}
public UserRole(Set<User> users, String role) {
this.users = users;
this.role = role;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_role_id",
unique = true, nullable = false)
public Integer getUserRoleId() {
return this.userRoleId;
}
public void setUserRoleId(Integer userRoleId) {
this.userRoleId = userRoleId;
}
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name="USER_ROLES",
joinColumns={@JoinColumn(name="user_role_id")},
inverseJoinColumns={@JoinColumn(name="username")})
public Set<User> getUsers() {
return this.users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
@Column(name = "role", nullable = false, length = 45)
public String getRole() {
return this.role;
}
public void setRole(String role) {
this.role = role;
}
@ManyToMany(mappedBy="userRoles",fetch = FetchType.EAGER)
public List<Menu> getMenus() {
return menus;
}
public void setMenus(List<Menu> menus) {
this.menus = menus;
}
}
User.java
package com.pmc.domain;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="users", catalog="test")
public class User {
private String username;
private String password;
private boolean enabled;
private Set<UserRole> userRole = new HashSet<UserRole>(0);
public User() {
}
public User(String username, String password, boolean enabled) {
this.username = username;
this.password = password;
this.enabled = enabled;
}
public User(String username, String password,
boolean enabled, Set<UserRole> userRole) {
this.username = username;
this.password = password;
this.enabled = enabled;
this.userRole = userRole;
}
@Id
@Column(name = "username", unique = true,
nullable = false, length = 45)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "password",
nullable = false, length = 60)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "enabled", nullable = false)
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@ManyToMany(mappedBy = "users")
public Set<UserRole> getUserRole() {
return this.userRole;
}
public void setUserRole(Set<UserRole> userRole) {
this.userRole = userRole;
}
}
我的查询是这样的。
select menu from User user, IN (user.userRole) roles, IN(roles.menus) menu where user.username = ?
它为用户提供了具有适当角色的所有必需菜单。但是每个菜单中的子菜单列表不会根据角色反映出来。
你能帮忙吗? 感谢