Hibernate如何使用常量作为复合外部引用的一部分 [英] Hibernate how to use a constant as part of composite foreign reference
问题描述
我有一个带有复合主键的主表 A,由 两个 列组成.其中一列是constant(下面代码中的THE CONSTANT VALUE").此表定义如下所示:
I have a master table A with a composite primary key, consisting of two columns. One of these columns is a constant ("THE CONSTANT VALUE" in the code below). This table definition looks like the following:
@Entity public class Master {
@Id
@Column(name = "SIGNIFICANT_KEY")
private String realKey;
@Id
@Column(name = "CONSTANT_KEY")
private String constantPartKey;
}
我有一个明细表 B,仅使用 一个(非常量)列引用主表 A.我想在两个表之间实现通常的 ManyToOne 和 OneToMany 关系.
I have a detail table B, referencing master table A using only one (non-constant) column. I want to implement usual ManyToOne and OneToMany relations between the two tables.
问题:如何使用 Hibernate 处理这种情况?
Question: How can I handle this situation with Hibernate?
我发现的主要参考的唯一解决方案依赖于使用公式:
The only solution for master reference I found relies on using formulas:
@Entity public class Detail {
@ManyToOne
@JoinColumnsOrFormulas(value={
@JoinColumnOrFormula(column=
@JoinColumn(name = "SIGNIFICANT_KEY",
referencedColumnName = "SIGNIFICANT_KEY",
insertable=false, updatable=false, nullable = false)),
@JoinColumnOrFormula(formula=
@JoinFormula(referencedColumnName="CONSTANT_KEY", value="'THE CONSTANT VALUE'"))
})
Master master;
}
现在我看到其他问题:由于我之前在这里报告的 java.lang.ClassCastException 问题,我无法在 OneToMany 关系中使用此字段:https://hibernate.onjira.com/browse/HHH-6811
Now I see other problem: I cannot use this field in OneToMany relation due to the java.lang.ClassCastException problem I've reported here earlier: https://hibernate.onjira.com/browse/HHH-6811
推荐答案
我真的很想知道在数据库中拥有一个常量列值会有什么帮助,但无论如何......
I really wonder how it can be helpful to have a constant column value in a database, but anyway...
我要做的就是只将非常量列映射为ID,并使常量列成为常规列,具有常量值:
What I would do is to only map the non-constant column as ID, and make the constant column a regular column, with a constant value:
@Entity
public class Strange {
@Id
private Long id;
@Column
private long constant = 345; // the constant that you want in your database
@OneToMany(mappedBy = "strange")
private Set<Detail> details;
...
// no setter for constant!
}
@Entity
public class Detail {
...
@ManyToOne
@JoinColumn(name = "strange_id")
private Strange strange;
}
如果其他实体使用其完整的复合键引用同一 Strange 实体,则只需执行以下操作:
If other entities reference this same Strange entity using its full composite key, then simply do the following:
@Entity
public class Strange {
@Id
@Column(name = "strange_id")
private Long id;
@Id
private long constant = 345; // the constant that you want in your database
@OneToMany(mappedBy = "strange")
private Set<Detail> details;
...
// no setter for constant!
}
@Entity
public class Detail {
...
@ManyToOne
@JoinColumn(name = "strange_id", referencedColumnName = "strange_id")
private Strange strange;
}
您可以使用 PK 以外的其他内容引用其他实体.另一个独特的列也可以.而且由于奇怪的 ID 是独一无二的,它符合要求.在这种情况下,必须使用 referencedColumnName
属性来指定引用的列名.
You can reference other entities using something other than their PK. Another unique column is also OK. And since strange_id is unique, it fits the bill. The referencedColumnName
attribute must be used to specify the referenced column name, in that case.
这篇关于Hibernate如何使用常量作为复合外部引用的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!