Hibernate将子实体中的ManyToOne映射到父实体的复合键中的字段

时间:2019-04-18 09:46:41

标签: java hibernate jpa

我有一个BookEntity类,该类的复合ID由Book表中的idpublishedDate列组成。

然后,我有一个BookMetaDataEntity类,它有自己的ID metaDataId

关系是一本书有许多元数据行。 BookEntity在第一个BookMetaDataEntity列的id和第二个bookId列的联接。反之亦然,我也在第二个实体上定义了JoinColum

BookEntity.java

@Entity
@Table(name = "Book")
public class Book {
    @EmbeddedId
    private CompositeId compositeId;

    @Column(name = "author")
    private String author;

    @OneToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY, targetEntity = BookMetaDataEntity.class)
    @JoinColumn(name = "id", referencedColumnName = "bookId", insertable = false, updatable = false)
    private List<BookMetaDataEntity> metaData;
}

CompositeId.java

@Embeddable
public class CompositeId implements Serializable {
    @Column(name = "id")
    private int id;

    @Column(name = "publishedDate")
    private int publishedDate;
}

BookMetaDataEntity.java

@Entity
@Table(name = "BookMetaData")
public class BookMetaDataEntity {
    @Id
    @Column(name = "metaDataId")
    private int metaDataId;

    @Column(name = "bookId")
    private int bookId;

    @Column(name = "attribute")
    private String attribute;

    @Column(name = "value")
    private String value;

    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY, targetEntity = BookEntity.class)
    @JoinColumn(name = "bookId", referencedColumnName = "id", insertable = false, updatable) 
    private Book book;
}

问题:引用BookEntity的BookMetaDataEntity.book的referencedColumnNames(id)未映射到单个属性。

因为id表中没有bookId列,所以连接仅在BookMetaData = publishedDate上发生。

如何从子实体映射回父实体的组合键中的字段?

0 个答案:

没有答案