使用多列连接进行一对一单向映射

时间:2018-06-19 09:02:38

标签: hibernate jpa spring-data hibernate-mapping

我正在尝试为表设计实体模型,当一个数据库表具有引用另一个表的外键时。

order_extensions
______________________________________
| id (pk)                             |
| type (fk constraint to 'orders')    |
| order_id (fk constraint to 'orders')|
| other fields                        |
---------------------------------------
orders
_______________
| type (pk)    |
| order_id (pk)|
| other fields |
----------------

OrderExtensionEntity我有:

@Id
@GeneratedValue
private Long id;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private OrderType type;

@Column(nullable = false)
private String orderId;

@OneToOne
@JoinColumns({@JoinColumn(name="orderId", referencedColumnName = "order_id"),@JoinColumn(name="type", referencedColumnName = "type")})
private OrderEntity order;

使用这种方法我收到错误

org.hibernate.MappingException: Unable to find column with logical name: order_id in org.hibernate.mapping.Table(orders) and its related supertables and secondary tables

映射有什么问题? 我的目标是通过OrderExtensionEntity字段从OrderEntityorder进行一对一的引用。此外,还不清楚是否可以在OrderEntity

的插入上编写OrderExtensionEntity
OrderExtensionEntity extension = new OrderExtensionEntity();
OrderEntity order = new OrderEntity();
...
extension.setOrder(order);
repository.save(extension);

0 个答案:

没有答案