Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid type safety warnings with Hibernate HQL results?

Tags:

java

generics

For example I have such query:

Query q = sess.createQuery("from Cat cat"); List cats = q.list(); 

If I try to make something like this it shows the following warning

Type safety: The expression of type List needs unchecked conversion to conform to List<Cat>   List<Cat> cats = q.list(); 

Is there a way to avoid it?

like image 389
serg Avatar asked Sep 22 '08 15:09

serg


People also ask

Is Hql type safe?

Both HQL and JPQL are non-type-safe ways to perform query operations. Criteria queries offer a type-safe approach to querying.

Does hibernate support union HQL query?

I know hibernate does not support union queries at the moment, right now the only way I see to make a union is to use a view table.

How does Hibernate HQL work?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.

What is Hql How does it different from SQL list its advantages?

Unlike SQL, HQL uses classes and properties in lieu of tables and columns. HQL supports polymorphism as well as associations, which in turn allows developers to write queries using less code as compared to SQL.


2 Answers

Using @SuppressWarnings everywhere, as suggested, is a good way to do it, though it does involve a bit of finger typing each time you call q.list().

There are two other techniques I'd suggest:

Write a cast-helper

Simply refactor all your @SuppressWarnings into one place:

List<Cat> cats = MyHibernateUtils.listAndCast(q);  ...  public static <T> List<T> listAndCast(Query q) {     @SuppressWarnings("unchecked")     List list = q.list();     return list; } 

Prevent Eclipse from generating warnings for unavoidable problems

In Eclipse, go to Window>Preferences>Java>Compiler>Errors/Warnings and under Generic type, select the checkbox Ignore unavoidable generic type problems due to raw APIs

This will turn off unnecessary warnings for similar problems like the one described above which are unavoidable.

Some comments:

  • I chose to pass in the Query instead of the result of q.list() because that way this "cheating" method can only be used to cheat with Hibernate, and not for cheating any List in general.
  • You could add similar methods for .iterate() etc.
like image 87
Matt Quail Avatar answered Sep 21 '22 07:09

Matt Quail


It is been a long time since the question was asked but I hope my answer might be helpful to someone like me.

If you take a look at javax.persistence api docs, you will see that some new methods have been added there since Java Persistence 2.0. One of them is createQuery(String, Class<T>) which returns TypedQuery<T>. You can use TypedQuery just as you did it with Query with that small difference that all operations are type safe now.

So, just change your code to smth like this:

Query q = sess.createQuery("from Cat cat", Cat.class); List<Cat> cats = q.list(); 

And you are all set.

like image 41
antonpp Avatar answered Sep 19 '22 07:09

antonpp