Hibernate如何使用一个常量作为复合外部引用的一部分 [英] Hibernate how to use a constant as part of composite foreign reference
问题描述
我有一个主表A,其中有一个由两个列组成的主键。其中一列是常量(下面代码中的常量值)。这个表的定义如下所示:
@Entity public class Master {
@Id
@Column (name =SIGNIFICANT_KEY)
private String realKey;
@Id
@Column(name =CONSTANT_KEY)
private String constantPartKey;
$ b我有一个详细表B,引用主表A只使用一个(非常量)列。我想在这两个表之间实现通常的ManyToOne和OneToMany关系。
问题:Hibernate如何处理这种情况?
主要参考的唯一解决方案是依赖使用公式:
@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 ='常值'))
})
主人主人;
$ b现在我看到另一个问题:我不能在OneToMany关系中使用这个字段,我之前在这里报告过的java.lang.ClassCastException问题: https://hibernate.onjira.com / browse / HHH-6811 解决方案我真的不知道如何有一个恒定的列值一个数据库,但无论如何...
我要做的只是将非常量列映射为ID,并将常量列作为常规列,常量值:
@Entity
public class Strange {
@Id
private Long id ;
@Column
private long constant = 345; //你在数据库中想要的常量
@OneToMany(mappedBy =strange)
private Set< Detail>细节;
...
//不设置常量!
@Entity
public class Detail {
...
@ManyToOne
@JoinColumn(name =strange_id)
私人奇怪的;
$ b如果其他实体使用完整的组合键引用同一个陌生实体,请执行以下操作:
$ $ p $ code $ @ $实际
公共类奇怪
@ $
@Column (name =strange_id)
私人长ID;
@Id
私有长常量= 345; //你在数据库中想要的常量
@OneToMany(mappedBy =strange)
private Set< Detail>细节;
...
//不设置常量!
}
@Entity
public class Detail {
...
@ManyToOne
@JoinColumn(name =strange_id,referencedColumnName =strange_id)
私人奇怪的;
$ b 你可以使用除PK以外的其他实体来引用它。另一个独特的列也行。而且由于strange_id是唯一的,它符合法案。在这种情况下, referencedColumnName
属性必须用于指定被引用的列名称。
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;
}
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.
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;
}
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...
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;
}
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;
}
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屋!