【问题标题】:JPA Criteria subqueries combined with disjunctionJPA Criteria 子查询结合析取
【发布时间】:2017-11-03 15:51:59
【问题描述】:

我对 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 条件查询来做到这一点?我尝试了很多方法,但它不起作用。

提前致谢。

【问题讨论】:

  • 尝试不使用不必要的子查询:select * from Person where Person.Id = 15625 or Person.Name = 'Name' or Person.Age = 28

标签: jpa subquery criteria


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2011-10-17
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2018-01-23
    相关资源
    最近更新 更多