我正在尝试为表设计实体模型,当一个数据库表具有引用另一个表的外键时。
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
字段从OrderEntity
到order
进行一对一的引用。此外,还不清楚是否可以在OrderEntity
OrderExtensionEntity
OrderExtensionEntity extension = new OrderExtensionEntity();
OrderEntity order = new OrderEntity();
...
extension.setOrder(order);
repository.save(extension);