質問

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);
役に立ちましたか?

解決

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.

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top