entityManager.createNativeQuery does not return a typed result

Viewed 2226

I need select native query with entityManager. I found method:

public Query createNativeQuery(String sqlString, Class resultClass);

And try use it:

Query query = em.createNativeQuery(readyQuery, SmevMessage.class);
return query.getResultList();

But IDE still highlights as a warning:

enter image description here

Why then does this method exist? and what should i do to fix it?

1 Answers

Which warning do you get? Does your IDE only warn you about the unchecked conversion of the returned List?

You can ignore that one. Unfortunately, the createNativeQuery method returns an untyped Query. There is no way you can fix that warning. If it really bothers you, you can suppress it with @SuppressWarnings("unchecked").

And please be aware that you need to provide an entity class as the value of the resultClass parameter, as I explained here. So, in your case, SmevMessage has to be an entity. If you want to map your query result to a DTO, you need to define an @SqlResultSetMapping.

Related