1 回答
![?](http://img1.sycdn.imooc.com/5458453d0001cd0102200220-100-100.jpg)
TA贡献1770条经验 获得超3个赞
您可以尝试“派生身份”映射:
@Entity(name = "EntityAAudit")
@Table(name = "audit")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("entity_a")
@DiscriminatorOptions(insert = true, force = true)
public class EntityAAudit extends Audit {
@EmbeddedId
@JsonUnwrapped
private AuditedId id;
@OneToOne
@JoinColumn(name = "item_id", nullable = false)
@MapsId("entityAId") // maps entityAId attribute of embedded id
private EntityA item;
@Embeddable
public static class AuditedId implements Serializable {
private Long entityAId; // corresponds to PK type of EntityA
@Column(name = "version", nullable = false)
private Long version;
}
}
注意上的@MapsId注释EntityAAudit.item。
另外,您将需要显式设置EntityAAudit.item和AuditedId.version。JPA不会为您神奇地确定和设置任何循环引用。
JPA 2.2规范的第2.4.1节中讨论了派生身份(带有示例)。
添加回答
举报