Criteria Query with BigMoney from Joda Money (Multi-Column field in Entity)
Asked Answered
T

0

6

I have a serious issue with Criteria Query.

My entity class looks like this:

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;
  ...
}

And I have another class Y which has a List<X> xs.

I also have a working normal JPA Query:

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

Now I'd like to transfer this to CriteriaQuery. I started with:

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);

I tried a few things, but none of them worked :( Here a few examples:

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

or:

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

or:

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

What am I doing wrong?


For now I'll stick with a little workaround solution, but it works as expexted:

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();
  }
}

Now the Criteria Query looks like:

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);
Telemachus answered 11/7, 2013 at 6:3 Comment(2)
Looks like you want to use JPA's CriteriaBuilder API with Hibernate proprietary annotation @ColumnsBechuana
Yeah, maybe that is/was the problem. I've posted some workaround (maybe the only possible)Telemachus

© 2022 - 2024 — McMap. All rights reserved.