Monday, March 19, 2012

JPA: Query, incompatible types compilation error

I have faced a problem while I am writing a generic JPA query like the following:

Compile it and you will get the following error in Jdeveloper 11.1.2.1.0
Error(200,37): incompatible types.

It works for Query.getResultList(), but it doesn't work for Query.getSingleResult().

A work around solves the problem which is, cast to T as (T) query.getSingleResult();

After investigation the perfect solution is to rewrite the following statement:

To be:

Use TypedQuery instead of Query and JDeveloper will remove all "Unchecked conversions from Object to T unsound but tolerated" warnings.

Note: that TypedQuery introduced in JPA 2.

No comments :

Post a Comment