Saturday, July 2, 2016

Select Long type variable in Querydsl

Leave a Comment

When i try to run this query :

Long count = ...; List<CritereItem> items= new JPAQuery(entityManager).from(foo)                      .list( new QCritereItem( foo.id, foo.name, count  )); 

I got compilation error because the constructor expect NumberPath<Long> not Long at the variable count , So how to select a variable in querydsl ?

I replace count in constructor by :

Expressions.numberTemplate(Long.class, count.toString()) 

But i got this execption

java.lang.IllegalArgumentException: java.lang.ClassCastException@14edf4 

1 Answers

Answers 1

You are trying to pass a constant through the QueryDSL constructor.

Check the Expressions static class for more informations: QueryDSL API Reference

NumberExpression<Long> count = Expressions.asNumber(...); List<CritereItem> items = new JPAQuery(entityManager).from(foo)                           .list( new QCritereItem( foo.id, foo.name, count )); 

Also note that if you want to aggregate a count, you can do it with foo.count()

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment