有一个表ORDERS,从中创建了视图ORDER_VIEW_A和ORDER_VIEW_B。
我创建了实体类OrderViewA和OrderViewB,它们中的每一个都映射到名为'TransactionRecord'的实体上。
这是@OneToOne关系。 TRANSACTION_RECORD表中有ORDER_ID列,而TransactionRecord实体中有字段orderId。
OrderViewA.id和OrderViewB.id的字段orderId相同,因为视图是从同一表中选择的。
我的问题是,如何在Hibernate中通过同一字段将OneToOne关系中的两个视图与另一个实体映射。
我的代码看起来像这样,它无论如何都无法工作,Hibernate总是以:
结尾org.hibernate.AnnotationException:引用的属性不是 (一个|很多)ToOne:com.example.app.model.TransactionRecord.orderId在 由com.example.app.model.views.OrderViewA.orderViewA映射
@Entity
@Immutable
@Table(name = "ORDER_VIEW_A")
public class OrderViewA {
@Id
@Column(name = "id")
private Long id;
...
@OneToOne(mappedBy = "orderId", fetch = FetchType.LAZY)
private IntegrationRecord orderARecord;
...
}
@Entity
@Immutable
@Table(name = "ORDER_VIEW_B")
public class OrderViewB {
@Id
@Column(name = "id")
private Long id;
...
@OneToOne(mappedBy = "orderId", fetch = FetchType.LAZY)
private IntegrationRecord orderBRecord;
...
}
@Entity
@Table(name = "TRANSACTION_RECORD")
public class TransactionRecord {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "order_id")
private Long orderId;
...
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id")
private OrderViewA orderViewA;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id")
private OrderViewB orderViewB;
...
}