Question

JPA has a method of the following signature:

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

How do I invoke this method when my resultClass type is, itself, type parameterized?

For example:

public class ResultDto<T> {
...
}

I tried the following but it didn't work:

createQuery(ResultDto<String>.class);
Was it helpful?

Solution

There is no Class instance for any specific instantiation of a generic type (e.g. ResultDto<String>, ResultDto<Integer>, ResultDto<MyObject>, ...).

No matter whether the class is generic or not there is always only one Class instance which represents raw version of the class. In your case, it is ResultDto.class.

OTHER TIPS

You can use createQuery(ResultDto.class)

The reason is when you are going to create a object of ResultDto , you will always create it using a type parameter for eg ResultDTo<String> rs = new ResultDTo<String>() So this type parameter will be considered in the <T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top