休眠/JPA 注释中的多列联接
2022-09-01 08:59:17
我有两个实体,我想通过多个列加入。这些列由两个实体共享的对象共享。在下面的示例中,只能有一个,但可以有多个 s(其中 是 的唯一键 )。下面是一个示例:@Embeddable
Foo
Bar
Bar
Foo
AnEmbeddableObject
Bar
@Entity
@Table(name = "foo")
public class Foo {
@Id
@Column(name = "id")
@GeneratedValue(generator = "seqGen")
@SequenceGenerator(name = "seqGen", sequenceName = "FOO_ID_SEQ", allocationSize = 1)
private Long id;
@Embedded
private AnEmbeddableObject anEmbeddableObject;
@ManyToOne(targetEntity = Bar.class, fetch = FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name = "column_1", referencedColumnName = "column_1"),
@JoinColumn(name = "column_2", referencedColumnName = "column_2"),
@JoinColumn(name = "column_3", referencedColumnName = "column_3"),
@JoinColumn(name = "column_4", referencedColumnName = "column_4")
})
private Bar bar;
// ... rest of class
}
和酒吧类:
@Entity
@Table(name = "bar")
public class Bar {
@Id
@Column(name = "id")
@GeneratedValue(generator = "seqGen")
@SequenceGenerator(name = "seqGen", sequenceName = "BAR_ID_SEQ", allocationSize = 1)
private Long id;
@Embedded
private AnEmbeddableObject anEmbeddableObject;
// ... rest of class
}
最后是类:AnEmbeddedObject
@Embeddable
public class AnEmbeddedObject {
@Column(name = "column_1")
private Long column1;
@Column(name = "column_2")
private Long column2;
@Column(name = "column_3")
private Long column3;
@Column(name = "column_4")
private Long column4;
// ... rest of class
}
显然,架构的规范化程度很差,这是一个限制,即 的字段在每个表中重复。AnEmbeddedObject
我遇到的问题是,当我尝试启动休眠时,我收到此错误:
org.hibernate.AnnotationException: referencedColumnNames(column_1, column_2, column_3, column_4) of Foo.bar referencing Bar not mapped to a single property
我尝试过标记JoinColumns不可插入和可更新,但没有运气。有没有办法用Hibernate/JPA注释来表达这一点?