I have this Bean object:
@Entity
@Table(name="a_table")
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="amount")
private int amount;
@ManyToOne
private B b;
@ManyToOne
private C c;
public MunicipalityInventory(int amount, B b, C c) {
this.amount = amount;
this.b = b;
this.c = c;
}
I try to bind my query result to object above. My first try with QueryDSL was something like this (it worked in that sense that the presentation of data was correct, but it didn't bind property's of query to the object, because it returned Tuple
which contained StringPath
, NumberExpression
and other weird data types for me)
public List<Tuple> findAll(){
QA a = QA.A;
QB b = QB.b;
QC c = QC.c;
JPAQuery query = new JPAQuery(entityManager);
return query.from(a)
.innerJoin(a.b, b)
.innerJoin(a.c, c)
.orderBy(c.name.asc())
.groupBy(c.name, a.c, c.name)
.list(a.amount.sum(), b.name, c.name);
}
As I said this worked, but now I am trying to convert that example to this (oh, and that new condition in groupBy
was needed to get this compile) so I can work right away with proper object:
return query.from(a)
.innerJoin(a.b, b)
.innerJoin(a.c, c)
.orderBy(b.name.asc())
.groupBy(c.name, a.amount, b.id, c.id)
.list(Projections.fields(A.class, a.amount.sum(), b, c));
When compiling this it will give error warning
java.lang.IllegalArgumentException: Unsupported expression sum(a.amount)
at com.mysema.query.types.QBean.createBindings(QBean.java:70)
Why's that? For my understanding this should work.
Querydsl can't determine from the sum() expression which field of A you want to populate.
Use an alias to define the target field:
a.amount.sum().as("fieldName")
Querydsl can handle only paths and alias expressions when binding to fields or properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With