带有Querydsl / JPQL / Hibernate

时间:2016-05-26 15:40:04

标签: hibernate subquery jpql querydsl case-when

我的应用程序中有三个实体,Customer,PriceLevel和TemporalCustomerPriceLevel。每个Customer都有一个名为defaultPriceLevel的字段,它直接引用PriceLevel。暂时,客户可以切换到另一个PriceLevel。为此,添加了TemporalCustomerPriceLevel的条目,该条目引用了PriceLevel和Customer。

实体类是:

@Entity
public class Customer implements Serializable {
...
  @ManyToOne
  @JoinColumn(name = "default_price_level_id")
  private PriceLevel defaultPriceLevel;

  @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<TemporalCustomerPriceLevel> temporalCustomerPriceLevels
      = new ArrayList<TemporalCustomerPriceLevel>();
...
}

@Entity
public class PriceLevel implements Serializable {
  ...
  @OneToMany(mappedBy = "priceLevel", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<TemporalCustomerPriceLevel> temporalCustomerPriceLevels;

  @OneToMany(mappedBy = "defaultPriceLevel", cascade = CascadeType.PERSIST)
  private List<Customer> customers;
  ...
}

@Entity
public class TemporalCustomerPriceLevel implements Serializable {
  ...
  @ManyToOne
  @JoinColumn(name = "customer_id")
  private Customer customer;

  @ManyToOne(cascade = CascadeType.PERSIST)
  @JoinColumn(name = "price_level_id")
  private PriceLevel priceLevel;
  ...
}

我现在想要查询有效价格水平,即。即defaultPriceLevel如果不是(活动,为简单起见省略)TemporalCustomerPriceLevel存在,而PriceLevel则由TemporalCustomerPriceLevel引用。

我使用Spring,JPA(Hibernate),MySql和Querydsl。在Querydsl,我写了以下内容:

QCustomer customer = QCustomer.customer;
QTemporalCustomerPriceLevel qt = QTemporalCustomerPriceLevel.temporalCustomerPriceLevel;

SimpleExpression<PriceLevel> cases = new CaseBuilder()
    .when(JPAExpressions.select(qt.count()).from(qt).where(qt.customer.eq(customer)).eq(1L))
    .then(
        JPAExpressions.select(qt.priceLevel).from(qt).where(qt.customer.eq(customer))
        )
    .otherwise(
        JPAExpressions.select(customer.defaultPriceLevel).from(customer)
        ).as("activePriceLevel");

JPAQuery<Tuple> query = factory.select(customer, cases).from(customer);

这会导致错误:

antlr.NoViableAltException: unexpected AST node: query
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1367) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
...

Querydsl生成的查询如下所示:

select customer, 
    (case when ((select count(temporalCustomerPriceLevel)
        from TemporalCustomerPriceLevel temporalCustomerPriceLevel
        where temporalCustomerPriceLevel.customer = customer) = ?1) 
    then (select temporalCustomerPriceLevel.priceLevel
        from TemporalCustomerPriceLevel temporalCustomerPriceLevel
        where temporalCustomerPriceLevel.customer = customer) 
    else (select customer.defaultPriceLevel
        from Customer customer) 
    end) as activePriceLevel
from Customer customer

这似乎有道理。此外,如果我用固定的PriceLevel对象替换两个JPAExpressions行,则查询将按预期运行。

所以主要的问题是:在then块的case - 子句中使用子查询是否存在约束?我的Querydsl出了什么问题?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我已经知道以下替代查询解决了我的问题。不过,如果有人知道then子句中的hibernate和JPQL子查询,我仍然会感兴趣。

JPAQueryFactory factory = new JPAQueryFactory(em);
QCustomer customer = QCustomer.customer;
QPriceLevel priceLevel = QPriceLevel.priceLevel;
QTemporalCustomerPriceLevel tmp = new QTemporalCustomerPriceLevel("tmp_qtcpl");
Date now = new Date();

JPAQuery<Tuple> query = factory.select(customer, priceLevel).from(customer, priceLevel).where(
    JPAExpressions.select(tmp).from(tmp)
    .where(tmp.validAfter.loe(now)
        .and(tmp.validUntil.after(now)
            .and(tmp.priceLevel.eq(priceLevel)
                .and(tmp.customer.eq(customer))))).exists()
    .or(
      customer.defaultPriceLevel.eq(priceLevel)
      .and(
          JPAExpressions.select(tmp).from(tmp)
          .where(tmp.validAfter.loe(now)
              .and(tmp.validUntil.after(now)
                  .and(tmp.customer.eq(customer)))).notExists()
          )
      )
    );