Type safety: Unchecked cast from List to List<String>

You can sure use TypedQuery with the parameter types is String in this case. So what you need is

TypedQuery<String> query = getEntityManager().createQuery(queryString, String.class); 

Please don't use @SuppressWarnings and don't type cast, because these are error-prone ways to do this. Follow the advice given in the following answer to a similar question and use TypedQuery : https://stackoverflow.com/a/21354639/3657198

TypedQuery<SimpleEntity> q = 
    em.createQuery("select t from SimpleEntity t", SimpleEntity.class);

List<SimpleEntity> listOfSimpleEntities = q.getResultList();
for (SimpleEntity entity : listOfSimpleEntities) {
    // do something useful with entity;
}

You will get this warning due runtime check cast.

Even if you use if(query.getResultList() instanceof List<?>) you will get this warning, so...

Tags:

Java