JPA Criteria子查询与析取相结合

时间:2017-11-03 15:52:00

标签: jpa subquery criteria

我对JPA标准API有一点问题。我有一个实体:

@Entity
@Table(name = "PERSON")
public class Person implements Serializable
{

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "person_gen")
@Column(name = "ID")
private Long id;

@Column(name = "NAME")
private String name;

@Column(name = "AGE")
private Integer = age;

public Long getId()
{
    return this.id;
}

public String getName()
{
    return this.name;
}

public Integer getAge()
{
    return this.age;
}
}

现在我想进行类似的查询:

select * from Person where 
(Person.Id) in (select Person.Id from Person where Person.Id = 15625) or 
(Person.Id) in (select Person.Id from Person where Person.Name = 'Name') or
(Person.Id) in (select Person.Id from Person where Person.Age = 28)

有些子查询带有'或'语句。如何使用JPA条件查询执行此操作?我尝试了许多不起作用的东西。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您的特定查询确实不必要地复杂。您的IN谓词都可以被删除并替换为以下更简单的谓词:

SELECT * 
FROM Person
WHERE Id = 12625
OR Name = 'Name'
OR Age = 28

这应该更容易转换为JPA条件查询。