-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: will Query.list() ever support generic collections?
PostPosted: Wed Oct 12, 2005 11:55 pm 
Beginner
Beginner

Joined: Thu Sep 16, 2004 8:14 pm
Posts: 27
In Hibernate 3.1 Query.list() returns a java.util.List.

I have this method:
Code:
public List<State> getAllStates() {
   Session session = HibernateUtil.getSession();
   Query query = session.createQuery("from State");
   query.setCacheable(true);
   return(query.list());
}

And Eclipse warns me about type safety in the return statement.

Even though I know that the query will never contain anything but State objects it would be nice if Query would return a List<State>.
Are there plans to implement that?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 5:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is not really possible the way the API is defined. You'd have to pass the returned class as a parameter of query.
The proper solution in your case is to actually cast the List into List<Blah> when returning it.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 8:12 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
You can silence Eclipse by using this annotation, since you're always going to have a safe cast:

Code:
@SuppressWarnings("unchecked")


-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 2:17 pm 
Beginner
Beginner

Joined: Thu Sep 16, 2004 8:14 pm
Posts: 27
emmanuel wrote:
The proper solution in your case is to actually cast the List into List<Blah> when returning it.


When I change the return statement to:

Code:
return((List<State>)query.list());


Eclipse shows this warning:

Type safety: The cast from List to List<State> is actually checking against the erased type List


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 15, 2005 5:10 am 
Newbie

Joined: Fri Oct 14, 2005 11:40 am
Posts: 7
If you really want to avoid the warnings, and you don't want to suppress them, the only way to do it is to copy the results of Query.list() manually into a new List<State>:

Quote:
List results = query.list();
List<State> newResults = new ArrayList<State>();
for (Object o : results) {
newResults.add((State) o);
}
return newResults;


That's the only "safe" way to do it as that's the only way that will guarantee that the resulting List<State> only contains State objects.

The reason you have to get the warnings is that at runtime a List and a List<State> object are both actually just plain List objects. So if query.list() returned a List ontaining anything other than State objects, the cast to List<State> would not cause any exception to be thrown - the program would continue fine, because no type checking of generics is done at runtime. Which might make debugging it difficult in future as you suddenly get a class cast exception popping up somewhere else in the program seemingly at random.

Of course you know that query.list() will only return State objects - but the compiler, and hence Eclipse, can't know that. Just suppress the warning - it has done its job, which is to ask you whether you are certain that the cast is valid.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.