我想为他们工作的每个组织为用户分配不同的角色。我从另一个 SO question中得到了实体 GrantedRole 的想法。
但是,当我尝试运行单元测试时,它们都无法加载应用程序上下文,因为 Hibernate 找不到实体角色。GrantedRole 已添加到我的 persistence.xml。我得到的错误是:
org.hibernate.AnnotationException:com.onior.modm.registration.domain.GrantedRole.role 上的 @OneToOne 或 @ManyToOne 引用了未知实体:com.onior.modm.registration.domain.GrantedRole$Role
当我将 GrantedRole.Role 添加到 persistence.xml 时,我得到:
javax.persistence.PersistenceException: [PersistenceUnit: modm-persistence] 类或包未找到
引起:java.lang.ClassNotFoundException:com.onior.modm.registration.domain.GrantedRole.Role
我的 GrantedRole 代码,我省略了 setter。DomainEntity 是我所有实体的超类。
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import org.springframework.security.core.GrantedAuthority;
@SuppressWarnings("serial")
@Entity
@Table(name = "role_assignments", uniqueConstraints = @UniqueConstraint(columnNames = {
"user", "organization"}))
public class GrantedRole extends DomainEntity implements GrantedAuthority {
private User user;
private Role role;
private Organization organization;
public enum Role {
USER, ORGADMIN, ADMIN
}
public GrantedRole() {
super();
}
public GrantedRole(User user, Role role, Organization organization) {
super();
this.user = user;
this.role = role;
this.organization = organization;
}
@ManyToOne
@NotNull
public User getUser() {
return user;
}
@ManyToOne
@NotNull
@Enumerated(EnumType.STRING)
public Role getRole() {
return role;
}
我该怎么做才能解决这个问题?我宁愿将 Role 保留为枚举,或者至少不将其设为枚举。