来自Joda Money的BigMoney的标准查询(实体中的多列字段)

时间:2013-07-11 06:03:02

标签: jpa criteria criteria-api hibernate-criteria criteriaquery

我对Criteria Query存在严重问题。

我的实体类看起来像这样:

class X {
  ...
    @Columns(columns = {
      @Column(name = "priceCurrency", nullable = false),
      @Column(name = "priceAmount", nullable = false)})
    @Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentBigMoneyAmountAndCurrency")
    @Basic(fetch = FetchType.EAGER)
    BigMoney price;
  ...
}

我有另一个Y级,List<X> xs

我还有一个正常的正常 JPA查询:

SELECT y
  FROM Y y
  LEFT JOIN y.xs x
  GROUP BY y
  ORDER BY SUM(y.price.amount)

现在我想把它转移到CriteriaQuery。我开始时:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Y> q = cb.createQuery(Y.class);
Root<Y> root = q.from(Y.class);

ListJoin<Y, X> j = root.join(Y_.xs, JoinType.LEFT);
q.groupBy(root);

我尝试了一些东西,但没有一个有效:(这里有几个例子:

Path<BigDecimal> x = j.get("priceAmount");

或:

Path<BigDecimal> x = j.get("price.amount");

或:

Path<BigDecimal> x = j.get(X_.price).get("amount");

我做错了什么?


现在我将坚持使用一个小解决方案,但它可以解决问题:

class X {
  ...
  @Column(name = "priceCurrency", nullable = false)
  @Basic(fetch = FetchType.EAGER)
  String priceCurrency;

  @Column(name = "priceAmount", nullable = false)
  @Basic(fetch = FetchType.EAGER)
  String priceAmount;

  ...

  public BigMoney getPrice() {
    return BigMoney.of(CurrencyUnit.of(priceCurrency), priceAmount);
  }

  public void setPrice() {
    this.priceCurrency = price.getCurrencyUnit().toString();
    this.priceAmount = price.getAmount();
  }
}

现在Criteria Query看起来像:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Y> q = cb.createQuery(Y.class);
Root<Y> root = q.from(Y.class);

ListJoin<Y, X> j = root.join(Y_.xs, JoinType.LEFT);
q.groupBy(root);
Path<BigDecimal> a = j.get(X_.priceAmount);

TypedQuery<Y> tq = entityManager.createQuery(q);

0 个答案:

没有答案