java @JoinColumns - 实体映射中的重复列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12414926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@JoinColumns - repeated column in mapping for entity
提问by Hanna
I may just be misunderstanding how @JoinColumns work, but I'm getting an error when I reuse the name attribute. But doesn't the name attribute map to a database column? Shouldn't I be able to reuse it?
我可能只是误解了 @JoinColumns 的工作方式,但是当我重用 name 属性时出现错误。但是 name 属性不是映射到数据库列吗?我不应该能够重复使用它吗?
There error I'm getting:
我得到了错误:
Repeated column in mapping for entity: data.model.DP column: division
The code:
代码:
@Column(name = "division", nullable = false)
private String division;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumns({
@JoinColumn(name = "division_labeldefintion", referencedColumnName = "labeldefinition"),
@JoinColumn(name = "division", referencedColumnName = "abbr")
})
private LabelFile divisionLabel;
What I'm trying to do is do a multi-column join from one table (which contains the division
column, and join it with the LabelFile
table.
我想要做的是从一个表(包含该division
列,并将其与LabelFile
表连接。
回答by juanignaciosl
If you want to reuse a column in a mapping you should mark one as insertable=false updatable=false
, for Hibernate to know which Java reference value is the relevant one.
如果您想在映射中重用一列,您应该将其标记为insertable=false updatable=false
,以便 Hibernate 知道哪个 Java 引用值是相关的。
More information on when to use those attributes is available in this question.
此问题中提供了有关何时使用这些属性的更多信息。
回答by deyo.vuk
When using the JoinColumn annotation you must be aware that depending on the type of association between two tables/entities the attributes "name" and "referencedColumnName" are changing the place where they referencing to. Check the JPA API, or look herefor more detailed description.
使用 JoinColumn 注释时,您必须注意,根据两个表/实体之间的关联类型,属性“name”和“referencedColumnName”正在更改它们引用的位置。检查 JPA API,或在此处查看更详细的说明。
In your case the "name" attribute of JoinColumn on "divisonLabel" property is referencing to the source table/entity. And "referencedColumnName" is referencing to the columns from LabelFile table/entity. So, you are trying to have two "division" columns in the source table/entity.
在您的情况下,“divisonLabel”属性上 JoinColumn 的“name”属性引用源表/实体。并且“referencedColumnName”正在引用来自 LabelFile 表/实体的列。因此,您试图在源表/实体中有两个“除法”列。
To have the mapping work switch values for "name" and "referencedColumnName".
为“name”和“referencedColumnName”进行映射工作切换值。