如何正确映射我的类(通过引用未知的目标实体属性进行映射)



我意识到已经有很多问题询问此异常消息,但是我已经阅读了它们,提供的修复程序没有帮助,并且它们未能解决我的情况:在使用类层次结构和歧视时遇到此异常消息。我正在使用 hibernate 5 注释并在初始化时收到以下异常(如果您不熟悉龙目岛,请原谅我的类/变量名称混淆和我对龙目岛注释的评论):

org.hibernate.AnnotationException: mappedby reference a unknown 目标实体属性: com.example.entity.C.a in com.example.entity.A.cs

以下是我的类(删除了不相关的字段)

@Entity
@Data // generates getters/setters for all fields
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class A implements Persistable<String> {
    @Id
    @NonNull
    private String id;
    @NonNull
    @OneToMany(mappedBy = "a", orphanRemoval = true, cascade = PERSIST)
    private Set<C> cs;
}

.

@Data //generates getters/setters for all fields as well as toString, equals, and hashCode implementations
@Entity
@NoArgsConstructor //generates a no args constructor (required by hibernate)
@AllArgsConstructor //generates a constructor with all fields present
@DiscriminatorColumn(discriminatorType = INTEGER, name = B.DISCRIMINATOR_NAME)
public abstract class B implements Persistable<UUID> {
    protected static final String DISCRIMINATOR_NAME = "direction";
    protected static final String DISCRIMINATOR_1_VALUE = "1";
    protected static final String DISCRIMINATOR_1_VALUE = "2";
    @Id
    @GeneratedValue
    @Column(columnDefinition = "uuid")
    private UUID id;
    @NonNull
    @ManyToOne(optional = false)
    @JoinColumn(nullable = false, updatable = false)
    private A a;
}

.

@Entity
@NoArgsConstructor //generates a no args constructor (required by hibernate)
@ToString(callSuper = true) //generates toString method which calls super.toString
@EqualsAndHashCode(callSuper = true) //generates equals and hashCode methods which call their respective methods in the super class
@DiscriminatorValue(B.SEND_DISCRIMINATOR_VALUE)
public class C extends B {
}

那么,我做错了什么?据我所知,一切都设置正确。

仔细观察后,我可以看到您需要将抽象类映射为@MappedSuperclass而不是@Entity才能使映射正常工作。

事实证明,我试图做的事情是不可能的,Hibernate开发人员不认为这是一个错误/问题。有关详细信息,请参阅 https://hibernate.atlassian.net/browse/HHH-7635。

最新更新