【问题标题】:AnnotationException: MappedBy reference an unknown target entity property - mapping an inherited classAnnotationException: MappedBy 引用未知的目标实体属性 - 映射继承的类
【发布时间】:2018-01-29 01:28:39
【问题描述】:

我有这样的继承实体

    @MappedSuperclass
    public class PostEntity {
    ...
        @ManyToOne
        @JoinColumn(name = "user_id")
        private UserEntity author;
    }

    @Entity
    @Table(name = "answers")
    public class AnswerEntity extends PostEntity {}

    @Entity
    @Table(name = "users")
    public class UserEntity {
    ...
        @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
        private List<AnswerEntity> answers;
    }

在编译过程中,他把我扔了

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.example.demo.jpa.entity.AnswerEntity.author in com.example.demo.jpa.entity.UserEntity.answers

不知道为什么他在映射过程中看不到author字段。

【问题讨论】:

标签: java spring hibernate spring-mvc spring-boot


【解决方案1】:

JPA 不支持,至少 Hibernate 不支持。您可以:

@OneToMany(mappedBy = "author")
private List<PostEntity> answers;

或将 UserEntity author 字段移至 AnswerEntity。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2017-11-13
    相关资源
    最近更新 更多