我对 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();
}
}
现在条件查询看起来像:
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);