43

Hey everyone, I'm new to persistence / hibernate and I need your help.

Here's the situation. I have a table that contains some stuff. Let's call them Persons. I'd like to get all the entries from the database that are in that table.

I have a Person class that is a simple POJO with a property for each column in the table (name, age,..)

Here's what I have :

Query lQuery = myEntityManager.createQuery("from Person")
List<Person> personList = lQuery.getResultList();

However, I get a warning saying that this is an unchecked conversion from List to List<Person>

I thought that simply changing the code to

Query lQuery = myEntityManager.createQuery("from Person")
List<Person> personList = (List<Person>)lQuery.getResultList();

would work.. but it doesn't.

Is there a way to do this ? Does persistence allow me to set the return type of the query ? (Through generics maybe ? )

5 Answers 5

90

As a newcomer to JPA was taking this as the definitive answer but then I found a better one via this question: Why in JPA EntityManager queries throw NoResultException but find does not?

Its as simple as as using TypedQuery instead of Query e.g.:

TypedQuery<Person> lQuery = myEntityManager.createQuery("from Person", Person.class);
List<Person> personList = lQuery.getResultList();
1
  • 1
    Query is an abstract type, so it can still be a Query here if you really want it. I prefer the one line implementation anyway: ``` List<Person> personList = myEntityManager.createQuery("from Person", Person.class).getResultList(); ```
    – aarowman
    Commented Mar 23, 2021 at 21:54
11

Note: This answer is outdated as of JPA 2.0, which allows you to specify the expected type. See this answer.


Suppressing the warning with

@SuppressWarnings("unchecked")
List<MyType> result = (List<MyType>) query.getResultList();

is the only solution to this problem I have ever seen. The suppression is ugly, but you can trust JPA to return the right type of object, so there is no need to check manually.

If you use polymorphisms and don't know the exact result type, the use of Generics with a bounded Class parameter is also a common pattern:

public List<T extends MyBaseType> findMyType(Class<T> type) {
    @SuppressWarnings("unchecked")
    List<T> result = (List<T>) this.entityManager.createQuery(
        "FROM " + type.getName())
        .getResultList();
    return result;
}
4

Well there is the java Collections class solution, but you didn't explain how your casting was failing, or if it was just giving a warning...

This is one way to validate this:

Collections.checkList(lQuery.getResultList(), Person.class);

But if you don't need to validate it:

@SuppressWarnings("unchecked") List<Person> personList = lQuery.getResultList();
1
  • I didn't explain how my casting was failing because it is not failing. It simply doesn't silence the Warning.. so it doesn't do anything.
    – GuiSim
    Commented Jun 5, 2009 at 19:36
2

Alternative way:

Query query = entityManager.createNativeQuery("SELECT * FROM person", Person.class);
List<Person> rows = query.getResultList();
1
1

I've been stuck with this problem for a while, too. You can iterate over the list, and check, but I'd prefer less noise. The shortest way I have seen getting around this is to silence the warning, but I am also very uncomfortable with that. I'd be interested to see other solutions.

@SuppressWarnings("unchecked") 
List<Person> personList = lQuery.getResultList();

Hmm, while researching I found an interesting post on java.net. I found the user comments particularly interesting.

Not the answer you're looking for? Browse other questions tagged or ask your own question.