Hibernate generic pattern problem
I am using Hibernate Template and have this code:
public List<Book> findBooksByName(String name) {
return getHibernateTemplate().find("FROM Book WHERE name = ?", name);
}
I thought it looked good. But when I ran this code, I got an error:
[ERROR] incompatible types
[ERROR] required: java.util.List<com.model.Book>
[ERROR] found: java.util.List<capture#1 of ?>
How can I fix this and get what I need? Thank you in advance!
source to share
As far as I can tell, you are expanding HibernateDaoSupport
as described in this example . getHibernateTemplate()
will return HibernateTemplate
no type specification. Which is okay since the HibernateTemplate has no type parameters.
So this find (...) method will return an object of List
objects. Actual Hibernate code can return a List of HibernateProxy
instances. This one HibernateProxy
is an auto-generated subclass of your domain class Book
in this case.
So, all you can do is put the result in the list you want:
public List<? extends Book> findBooksByName(String name) {
return (List<? extends Book>) getHibernateTemplate().find("FROM Book WHERE name = ?", name);
}
This will make your List
reading efficient. This is the price we pay for the convenience of the ORM.
source to share