I'm new to Hibernate.
I'm trying to get a list of first name and last name of all administrators.
There are two warnings in my following code. I already tried to search a lot online.
1) Query is a raw type. References to generic type Query should be parameterized.
2) The method list() from the type Query is deprecated.
public List<Object> loadAllAdmins() {
List<Object> allAdmins = new ArrayList<Object>();
try {
HibernateUtil.beginTransaction();
Query q = currentSession.createQuery("SELECT admin.firstName, admin.lastName from AdminBean admin");
allAdmins= q.list();
HibernateUtil.commitTransaction();
} catch (HibernateException ex) {
System.out.println("List<AdminBean> loadAllPersons: HibernateException");
}
return allAdmins;
}
But I see sample code like this all over the web. How should I solve these two problems?
Update
I just tried to use Criteria as suggested. It also says the list() method is deprecate for Criteria... It seems that a lot of methods are deprecate for both Query and Criteria, including uniqueResult() and others... Any suggestion how I should replace them?
public List<Admin> getAdmins() {
List<Admin> AdminList = new ArrayList<Admin>();
Session session = factory.openSession();
for (Object oneObject : session.createQuery("FROM Admin").getResultList()) {
AdminList.add((Admin)oneObject);
}
session.close();
return AdminList;
}
The warnings came from "Type Inference".
I had the similar problem. However, I found a solution without "SuppressWarnings".
Recently, I found out a shorter way to code the same things without type inference.
public List<Admin> getAdmins() {
Session session = factory.openSession();
TypedQuery<Admin> query = session.createQuery("FROM Admin");
List<Admin> result = query.getResultList();
session.close();
return result;
}
Hope it helps.
Don't import QUERY
from org.hibernate
(As its Deprecated now). Instead import from “org.hibernate.query” .Eclipse reference
I tested other methods of hibernate javadoc and i came up with getResultList()
method of the TypedQuery<T>
interface. Example:
public List<Admin> getAdmins() {
Session session = factory.openSession();
@SuppressWarnings("unchecked")
List<Admin> result = session.createQuery("FROM Admin").getResultList();
session.close();
return result;
}
The difference is that the returned type of createQuery
is not Query
but a subinterface called TypedQuery<T>
. Because it is typed, it also fixes the "Query is a raw type" warning.
With this solution you may get a Type Safety warning, which can be solved by either casting each object explicitly or by adding @SuppressWarnings("unchecked")
Regarding criterias see hibernate user guide
Nevertheless, i am wondering why the tutorials-page of hibernate is not adjusted.
Did you try use criteria?
See that example:
public List<NoteVO> listNotes() {
Session session = HibernateSessionFactory.getSession();
Criteria crit = session.createCriteria(NoteVO.class);
List<NoteVO> listNotes = crit.list();
session.close();
return listNotes;
}
According to Hibernate Create Query , there are two types of createQuery()
methods :
Query createQuery(java.lang.String queryString )
Create a Query instance for the given HQL/JPQL query string.
<R> Query<R> createQuery(java.lang.String queryString, java.lang.Class<R> resultClass)
Create a typed Query instance for the given HQL/JPQL query string.
In your case you used the first Query
and list() method is @deperecated
.
You can user getResultList()
instead of list()
for Multiple results :
List<Object> loadAllAdmins() {
List<Object> allAdmins = new ArrayList<Object>();
try {
HibernateUtil.beginTransaction();
allAdmins= List<Object> currentSession.createQuery("SELECT admin.firstName, admin.lastName from AdminBean admin").getResultList();
HibernateUtil.commitTransaction();
} catch (HibernateException ex) {
System.out.println("List<AdminBean> loadAllPersons: HibernateException");
}
return allAdmins;
}
For single result you can use getSingleResult()
:
Admin= (Object) currentSession.createQuery("SELECT admin.firstName, admin.lastName from AdminBean admin").getSingleResult();
PS: I used Object
based on your type Object
,you can cast the result according to used type.
Although late to the party but may be worth noting the overloaded createQuery method (to avoid warnings altogether):
public List<Admin> loadAllAdmins() {
try {
HibernateUtil.beginTransaction();
List<Admin> admins = currentSession.createQuery("SELECT admin.firstName, admin.lastName from AdminBean admin", Admin.class).getResultList();
HibernateUtil.commitTransaction();
return admins;
} catch (HibernateException ex) {
System.out.println("List<AdminBean> loadAllPersons: HibernateException");
return new ArrayList<>();
}
}
来源:https://stackoverflow.com/questions/38460764/hibernate-5-2-version-a-lot-of-query-methods-deprecate