java JPA/Hibernate 加入常量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28818511/
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
JPA/Hibernate Join On Constant Value
提问by EvilJoe
I am trying to join to different entities from the same table using a constant value in the join statement. In SQL, I would do something like this...
我正在尝试使用 join 语句中的常量值从同一个表中加入不同的实体。在 SQL 中,我会做这样的事情......
SELECT *
FROM owner o
JOIN types t on t.owner_id = o.id AND t.type = 'A'
-- ^^^^^^^^^^^^^^^^ THIS IS WHAT I AM TRYING TO REPLICATE
In Java + JPA/Hibernate, I am trying to do something like this...
在 Java + JPA/Hibernate 中,我正在尝试做这样的事情......
@Entity
@Table(name = "OWNER")
public class Owner {
@Id
@Column(name="ID")
private Long id
@OneToOne(mappedBy = "owner", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "ID", referencedColumnName = "ID"),
@JoinColumn(constantValue = "A", referencedColumnName="type")})
// ^^^^^^^^^^^^^^^^^^^ I AM LOOKING FOR SOMETHING THAT DOES THIS.
// constantValue IS NOT A VALID ARGUMENT HERE.
private TypeA inspectionSnapshot;
@OneToOne(mappedBy = "owner", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "ID", referencedColumnName = "ID"),
@JoinColumn(constantValue = "B", referencedColumnName="type")})
private TypeB inspectionSnapshot;
/* Getters & Setters ... */
}
@Entity
@Table(name = "TYPES")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class BaseType {
@Id
@OneToOne
@JoinColumn(name = "OWNER_ID", referencedColumnName="ID")
private Owner id;
@Id
@Column(name = "TYPE")
private char type
/* Getters & Setters ... */
}
@Entity
@DiscriminatorValue("A")
public class TypeA extends BaseType {
/* All functionality in BaseType */
}
@Entity
@DiscriminatorValue("B")
public class TypeA extends BaseType {
/* All functionality in BaseType */
}
Thanks in advance!
提前致谢!
采纳答案by Kikin-Sama
You're looking at non-standard joins. Here's the documentation for treating such a case:
您正在查看非标准连接。这是处理这种情况的文档:
http://docs.oracle.com/cd/E13189_01/kodo/docs40/full/html/ref_guide_mapping_notes_nonstdjoins.html
http://docs.oracle.com/cd/E13189_01/kodo/docs40/full/html/ref_guide_mapping_notes_nonstdjoins.html
Hope it helps!
希望能帮助到你!
回答by Dmitry
Try to specify a constant as the value of the formula
尝试指定一个常量作为公式的值
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula=@JoinFormula(value="'A'", referencedColumnName="type")),
@JoinColumnOrFormula(column = @JoinColumn("id", referencedColumnName="id"))
})
private TypeA inspectionSnapshot;
回答by David Levesque
If you don't mind using Hibernate-specific annotations you could try with the @WhereJoinTable
annotation, e.g.:
如果您不介意使用特定于 Hibernate 的注释,您可以尝试使用@WhereJoinTable
注释,例如:
@OneToOne(mappedBy = "owner", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "ID", referencedColumnName = "ID")
@WhereJoinTable(clause = "TYPE = 'A'")
private TypeA inspectionSnapshot;
Note that the clause
attribute must contain SQL, not JPQL, so you need to use the database column name instead of the JPA entity field name.
请注意,该clause
属性必须包含 SQL,而不是 JPQL,因此您需要使用数据库列名称而不是 JPA 实体字段名称。