@ElementCollection上的querydsl子查询

时间:2017-10-11 07:16:12

标签: java spring-data-jpa querydsl

我遇到QueryDsl和@ElementCollection

的问题

说我有一个根类

@Entity
public class Root {
    @Id
    private String id = UUID.randomUUID().toString();

    @ElementCollection
    @CollectionTable(name = "VAL", joinColumns = @JoinColumn(name = "ROOT_ID"))
    @OrderBy("prop")
    public SortedSet<Val> vals = new TreeSet<>();

和值

@Access(AccessType.FIELD)
@Embeddable
public class Val implements Comparable<Val>{
   public String prop;
   public LocalDateTime timestamp;

  @Override
  public int compareTo(Val o) {
      return prop.compareTo(o.prop);
  }

}

我想要执行的SQL查询就像:

select * FROM VAL v, ROOT r
    WHERE
        r.ID = v.ROOT_ID
        and v.TIMESTAMP in (select max(va.TIMESTAMP) from VAL va GROUP BY va.ROOT_ID)
        and v.PROP = 'value'

当我尝试将其转换为querydsl时,问题是QVal不是'EntityPathBase'而是'BeanPath'。并且它不能用于from。

querydsl就像是

QRoot root = QRoot.root;

JPQLQuery<Root> value = JPAExpressions.select(root)
    .from(root)
    .innerJoin(root.vals, QVal.val)
    .where(QVal.val.timestamp.in(
        JPAExpressions.select(QVal.val.timestamp.max())
                    .from(QVal.val)   <---- does not work !
                    .groupBy(QVal.val))
                    .and(QVal.val.prop.eq("value"))
            );

repository.findAll(root.in(value));

0 个答案:

没有答案