JPA / Hibernate加入常量值

我正在尝试使用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

在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 */

}

提前致谢!

回答:

您正在查看非标准联接。这是处理这种情况的文档:

http://docs.oracle.com/cd/E13189_01/kodo/docs40/full/html/ref_guide_mapping_notes_nonstdjoins.html

希望能帮助到你!

以上是 JPA / Hibernate加入常量值 的全部内容, 来源链接: utcz.com/qa/406406.html

回到顶部