public class WebUser {
@ElementCollection(targetClass = SecurityRole.class)
@Enumerated(EnumType.STRING)
@CollectionTable(name = "webRole_SecurityRole")
@Column(name = "roles")
private List<SecurityRole> securityRoles = new ArrayList<SecurityRole>();
}
我使用这个命名查询检索用户安全角色:
@NamedQuery(name=WebUser.GET_WEBUSER_SECURITYROLES,
query="select w.securityRoles from WebUser w where w.id=:id")
并在使用 foreach 循环遍历角色以显示它们时:
List<SecurityRole> securityRoles = webUserFacade
.findUserSecurityRoles("51");
for (int i = 0; i < securityRoles.size(); i++) {
System.out.println(securityRoles.get(i).getTitleResourceName());
}
我得到了这个异常:java.lang.ClassCastException: java.lang.String cannot be cast to com.myproject.SecurityRole
安全角色枚举:
public enum SecurityRole {
private SecurityRole(String titleResourceName, String detailResourceName,
String url, String iconName, SecurityRoleGroup securityRoleGroup) {
this.detailResourceName = detailResourceName;
this.url = url;
this.titleResourceName = titleResourceName;
this.iconName = iconName;
this.securityRoleGroup = securityRoleGroup;
}
//gettters
}
我的外观中的 findUserSecurityRoles 方法:
public List<SecurityRole> findUserSecurityRoles(String id) {
return getEntityManager().createNamedQuery(
WebUser.GET_WEBUSER_SECURITYROLES, SecurityRole.class).setParameter("id", id)
.getResultList();
}