Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "proper" way to cast Hibernate Query.list() to List<Type>?

I'm a newbie with Hibernate, and I'm writing a simple method to return a list of objects matching a specific filter. List<Foo> seemed a natural return type.

Whatever I do, I can't seem to make the compiler happy, unless I employ an ugly @SuppressWarnings.

import java.util.List; import org.hibernate.Query; import org.hibernate.Session;  public class Foo {      public Session acquireSession() {         // All DB opening, connection etc. removed,         // since the problem is in compilation, not at runtime.         return null;     }      @SuppressWarnings("unchecked") /* <----- */      public List<Foo> activeObjects() {         Session s = acquireSession();         Query   q = s.createQuery("from foo where active");         return (List<Foo>) q.list();     } } 

I would like to get rid of that SuppressWarnings. But if I do, I get the warning

Warning: Unchecked cast from List to List<Foo> 

(I can ignore it, but I'd like to not get it in the first place), and if I remove the generic to conform to .list() return type, I get the warning

Warning: List is a raw type. References to generic type List<E> should be parameterized. 

I noticed that org.hibernate.mapping does declare a List; but it is a different type altogether - Query returns a java.util.List, as a raw type. I find it odd that a recent Hibernate (4.0.x) would not implement parameterized types, so I suspect that it's me instead doing something wrong.

It looks very much like Cast Hibernate result to a list of objects, but here I have no "hard" errors (the system knows type Foo, and I'm not using a SQLQuery but a straight Query). So no joy.

I have also looked at Hibernate Class Cast Exception since it looked promising, but then I realized that I do not actually get any Exception... my problem is just that of a warning - a coding style, if you will.

Documentation on jboss.org, Hibernate manuals and several tutorials do not seem to cover the topic in such detail (or I didn't search in the right places?). When they do enter into detail, they use on-the-fly casting - and this on tutorials that weren't on the official jboss.org site, so I'm a bit wary.

The code, once compiled, runs with no apparent problem... that I know of... yet; and the results are the expected ones.

So: am I doing this right? Am I missing something obvious? Is there an "official" or "recommended" Way To Do It?

like image 279
LSerni Avatar asked Apr 09 '13 21:04

LSerni


People also ask

What does Query List() return in hibernate?

list. Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].

How SQL creates query in hibernate?

For Hibernate Native SQL Query, we use Session. createSQLQuery(String query) to create the SQLQuery object and execute it. For example, if you want to read all the records from Employee table, we can do it through below code. When we execute above code for the data setup we have, it produces following output.

What is list hibernate?

A List is a java collection that stores elements in sequence and allow duplicate elements. The user of this interface has precise control over where in the list, each element is inserted. The user can access elements by their integer index, and search for elements in the list.


1 Answers

Short answer @SuppressWarnings is the right way to go.

Long answer, Hibernate returns a raw List from the Query.list method, see here. This is not a bug with Hibernate or something the can be solved, the type returned by the query is not known at compile time.

Therefore when you write

final List<MyObject> list = query.list(); 

You are doing an unsafe cast from List to List<MyObject> - this cannot be avoided.

There is no way you can safely carry out the cast as the List could contain anything.

The only way to make the error go away is the even more ugly

final List<MyObject> list = new LinkedList<>(); for(final Object o : query.list()) {     list.add((MyObject)o); } 
like image 193
Boris the Spider Avatar answered Oct 13 '22 07:10

Boris the Spider