【问题标题】:Hibernate AnnotationException unknown entity on Enum枚举上的休眠 AnnotationException 未知实体
【发布时间】:2013-06-10 21:44:02
【问题描述】:

我想为他们工作的每个组织为用户分配不同的角色。我从another SO question 得到了实体 GrantedRole 的想法。

但是,当我尝试运行单元测试时,它们都无法加载应用程序上下文,因为 Hibernate 找不到实体角色。 GrantedRole 已添加到我的 persistence.xml。 我得到的错误是:

org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne on com.onior.modm.registration.domain.GrantedRole.role 引用了一个 未知实体: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 代码,我省略了设置器。 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 保留为 enum,或者至少不将其设为 enum。

【问题讨论】:

    标签: hibernate jpa enums


    【解决方案1】:

    啊,我想我知道出了什么问题。我将 GrantedRole-Role 声明为 ManyToOne 关系,但它期望 Role 是一个实体,但事实并非如此。我删除了@ManyToOne 部分,现在一切正常:)

    事实证明,在发布新问题之前,我应该再花半个小时阅读博客文章和 SO 问题...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-21
      • 2015-07-28
      • 2013-07-03
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多