我有一个BookEntity
类,该类的复合ID由Book表中的id
和publishedDate
列组成。
然后,我有一个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
上发生。
如何从子实体映射回父实体的组合键中的字段?