使用JPA CriteriaBuilder的多级子查询

时间:2018-11-28 18:26:23

标签: jpa

我有以下JPA实体

@Entity
@Table(name="application_user")
public class ApplicationUser {
    @Id
    @Column(name="user_id")
    private String userid;

    @Column(name="last_write_time")
    private Instant lastWrite;

    //other fields omitted
}

@Entity
@Table(name="demographic")
public class Demographic {
    @Id
    @Column(name="user_id")
    private String userid;

    //primary key is a foreign key link
    @OneToOne
    @PrimaryKeyJoinColumn(name="user_id", referencedColumnName="user_id")
    private ApplicationUser user;

    //other fields omitted
}

我的目标是检索包含用户的所有人口统计信息,其中最后一次写入时间是该列中的最大值。我非常想使用JPA CriteriaBUilder编写以下SQL

select * from demographic where
  userid in (
    select userid from application_user where
      last_write in (
        select max(last_write) from application_user
      )
  )

我尝试编写以下CriteriaBuilder代码以实现此目标,并且编译成功。注意,我正在使用生成的Metamodel类。

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Demographic> c = cb.createQuery(Demographic.class);
Root<Demographic> root = c.from(Demographic.class);
root.fetch(Demographic_.user, JoinType.INNER);

Subquery<Instant> sqLatestUsers = c.subquery(Instant.class);
Root<ApplicationUser> subRootLatestUsers = sqLatestUsers.from(ApplicationUser.class);
sqLatestUsers.select(cb.greatest(subRootLatestUsers.<Instant>get(ApplicationUser_.LAST_WRITE)));
Predicate predicateLatestUsers = subRootLatestUsers.get(ApplicationUser_.LAST_WRITE).in(sqLatestUsers);

Subquery<ApplicationUser> sq = c.subquery(ApplicationUser.class);
Root<Demographic> subRoot = sq.from(Demographic.class);
sq.select(subRoot.<ApplicationUser>get(Demographic_.USER)).where(predicateLatestUsers);
Predicate containsUsers = subRoot.get(Demographic_.USER).in(sq);

c.select(root).where(containsUsers);

代码可以在Wildfly 14中编译并成功部署,但是当我执行代码时,会收到以下错误(带有空格以提高可读性):

Invalid path: 'generatedAlias2.user' :  Invalid path: 'generatedAlias2.user'
...
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'generatedAlias2.user' [
select generatedAlias0 from com.company.model.Demographic as generatedAlias0 
  inner join fetch generatedAlias0.user as generatedAlias1 
    where generatedAlias2.user in (
      select generatedAlias2.user from com.company.model.Demographic as generatedAlias2 where generatedAlias3.lastWrite in (
        select max(generatedAlias3.lastWrite) from com.company.model.StarfishUser as generatedAlias3
      )
    )
]

JPA规范是否允许链接子查询(嵌套子查询)?我找到语法上正确但实际上不允许的东西吗?

1 个答案:

答案 0 :(得分:0)

我弄清楚了如何使子查询正常工作。首先是我更新的Utility方法

public static <R, T> Subquery<T> getLatestSubelement(CriteriaBuilder cb, CriteriaQuery<R> c, Class<T> clazz, SingularAttribute<T, Instant> attribute) {
    //Get latest timestamp
    Subquery<Instant> sq = c.subquery(Instant.class);
    Root<T> subRoot = sq.from(clazz);
    sq.select(cb.greatest(subRoot.<Instant>get(attribute)));

    //Get object with the latest timestamp
    Subquery<T> sq2 = c.subquery(clazz);
    Root<T> subRoot2 = sq2.from(clazz);
    sq2.where(subRoot2.get(attribute).in(sq));
    return sq2;
}

这是使用实用程序方法的代码

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Demographic> c = cb.createQuery(Demographic.class);
Root<Demographic> root = c.from(Demographic.class);
joinType = JoinType.INNER;
//use fetch instead of join to prevent duplicates in Lists
root.fetch(Demographic_.user, joinType);

Subquery<ApplicationUser> sq = JpaUtil.getLatestSubelement(cb, c, ApplicationUser.class, ApplicationUser_.lastWrite);
c.where(root.get(Demographic_.user).in(sq));
TypedQuery<Demographic> q = em.createQuery(c);
Stream<Demographic> stream = q.getResultStream();