[英]Hibernate associate two entities via non-id column
I am facing the following scenario: 我面临以下情况:
Table Store: |Id|...|CostCenterNumber| 表存储:| Id | ... | CostCenterNumber |
Table MasterData: |dennskdnr|...| 表MasterData:| dennskdnr | ... |
My current mapping looks like the following 我当前的映射如下所示
@Entity
@Table(name = "Store")
public class Store implements Identifiable {
[...]
@OneToOne(optional = true)
@JoinColumn(name = "CostCenterNumber", insertable = false, updatable = false)
private MasterData masterData;
[...]
}
and 和
@Entity
@Table(name = "MasterData")
public class MasterData {
[...]
@OneToOne(optional = true)
@JoinColumn(name = "dennskdnr")
private Store store;
[...]
}
Leading me to the following exception: 引导我到以下异常:
org.hibernate.TypeMismatchException: Provided id of the wrong type for class datamodel.Store. org.hibernate.TypeMismatchException:为类datamodel.Store提供了错误类型的id。 Expected: class java.lang.String, got class java.lang.Integer 预期:类java.lang.String,得到类java.lang.Integer
Here's how the association should be mapped: 以下是关联应该如何映射:
@Entity
@Table(name = "Store")
public class Store implements Identifiable {
@OneToOne(optional = true)
@JoinColumn(name = "CostCenterNumber", referencedColumnName="dennskdnr")
private MasterData masterData;
}
@Entity
@Table(name = "MasterData")
public class MasterData {
@OneToOne(optional = true, mappedBy = "masterData")
private Store store;
}
Remember: in a bidirectional association, there is always an owner side, which defines how the association is mapped, and an inverse side, which uses the must use the mappedBy
attribute to say: I'm the inverse side, look at the "masterData" attribute in the other entity to know how this association is mapped . 记住:在双向关联中,总是有一个所有者端,它定义了关联的映射方式,还有一个反面,它使用必须使用mappedBy
属性来说: 我是反面,看看“masterData” “另一个实体中的属性,以了解此关联的映射方式 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.