Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering by JPAExpressions.type throws IllegalArgumentException #1747

Closed
bekoeppel opened this issue Jan 14, 2016 · 3 comments
Closed

Filtering by JPAExpressions.type throws IllegalArgumentException #1747

bekoeppel opened this issue Jan 14, 2016 · 3 comments

Comments

@bekoeppel
Copy link

In a model with Single Table Inheritance, I would like to select objects based on their type. For this, I use the JPAExpressions.type expression:

    JPAQuery query = new JPAQuery(entityManager);
    query.from(parent)
        .where(JPAExpressions.type(parent).in(typeList)
    );

However, when my typeList is a List<String> of class names, I get:

    java.lang.IllegalArgumentException: Parameter value [ChildA] did not match expected type [java.lang.Class (n/a)]
    at org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:874)
    at org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:80)
    at org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:248)
    at org.hibernate.jpa.internal.QueryImpl$JpaPositionalParameterRegistrationImpl.bindValue(QueryImpl.java:337)
    at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:674)
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:198)
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:311)
    at com.sun.proxy.$Proxy99.setParameter(Unknown Source)
    at com.mysema.query.jpa.impl.JPAUtil.setConstants(JPAUtil.java:55)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:128)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:94)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.list(AbstractJPAQuery.java:245)
    at ch.benediktkoeppel.test.querydsltype.ParentRepositoryCustomImpl.findByType(ParentRepositoryCustomImpl.java:18)
    ...

I think the typeList should be List<Class>, but the method signature does not allow this.

It could also be that I use the JPAExpressions.type wrongly. I asked a question on StackOverflow but did not get an answer: http://stackoverflow.com/questions/34568368/where-condition-based-on-jpas-type-with-querydsl

@gkprasad0308
Copy link

I am also struggling with this issue, did we have any solution to solve this problem?

@manorfm
Copy link

manorfm commented Oct 1, 2017

...
where(parent.instanceOfAny([QTypeA.getType(), QTypeB.getType()]))
...

@jwgmeligmeyling
Copy link
Member

jwgmeligmeyling commented Jan 1, 2021

Try Expressions.operation(Class.class, JPQLOps.TYPE, parent).in(typeList).

I agree that JPAExpressions.type could have returned this result for you, but that probably gives new issues when its used in the select clause of a query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants