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无法强制转换为com.myproject.SecurityRole
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();
}