views:

1636

answers:

5

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

A: 

I apologize if there is a better way to do this, but you could simply

List<Person> personList = new LinkedList<Person>();
for(Object person : lQuery.getResultList())
{

if(person instanceof Person)
{
personList.add(person);
}

}
instanceofTom
+1  A: 

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.

itrekkie
+1  A: 

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();
Petriborg
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
+5  A: 

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;
}
Henning
A: 

Hi, I'm having a similar problem, but the SupressWarnings solution does not work. Actually, I'm not getting a warning but a ClassCastException.

    Query q = em.createNativeQuery("SELECT * FROM PERSON");
    @SuppressWarnings("unchecked")
    List<Person> personList = List<Person> q.getResultList();

So far so good, then at the "for" line, the exception appears and my program can't continue.

    for (Person p : personList) {
        System.out.println(p.toString());
    }

What's even weirder, if I keep the result list as List and then ask if (obj instanceof Person), I get false. I am using Hibernate JPA that comes with NetBeans 6.8.

Lukfi