【问题标题】:JPA Hibernate OneToMany Enum - Unmapped Class ErrorJPA Hibernate OneToMany 枚举 - 未映射的类错误
【发布时间】:2020-04-14 12:46:10
【问题描述】:

我正在尝试使用 JPA/Hibernate 在我的实体类中存储一组枚举,但是当我实际上有一个枚举时,我不断收到关于“未映射的类”的错误。我做错了什么?

我得到的错误:(格式化为多行以便于阅读)

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'entityManagerFactory' defined in class path resource 
 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: 

Invocation of init method failed; 
nested exception is org.hibernate.AnnotationException: 
Use of @OneToMany or @ManyToMany targeting an unmapped class: path.to.my.MyEnum

声明

@Entity(name=MyEntity)
@Table(name=myentity)
public class MyEntity {

    ....

    @ElementCollection
    @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true)
    @Enumerated(EnumType.STRING)
    private Set<MyEnum> something;

    ....
}

我的枚举:

public enum MyEnum {

    ONE("one string"),
    TWO("two string"),
    THREE("three string");

    private final String name;

    private MyEnum(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return name;
    }
}

【问题讨论】:

    标签: java hibernate jpa enums


    【解决方案1】:

    您只能对使用@Entity 注释的类使用@OneToMany 注释。删除注释,它应该可以工作。

    【讨论】:

    • 这有帮助!我现在还可以从我的应用程序中“保存”一个对象,当我尝试从存储中获取它时,我收到另一个错误:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role ....MyClass.something ,不能初始化代理 - 没有会话;知道这是否相关吗?
    • 更改 @ElementCollection (fetch = FetchType.EAGER) 修复了它。感谢您的帮助!
    • @Cribber 如果你想更好地控制表名和 JPA/Hibernate 创建的列来保存你的元素集合,你可以使用 @CollectionTable@Column 注释来做到这一点。跨度>
    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 2016-12-13
    • 2023-03-20
    相关资源
    最近更新 更多