Hibernate 5.2 version -> A lot of Query methods deprecate?
Solution 1
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.
Solution 2
Don't import QUERY
from org.hibernate
(As its Deprecated now). Instead import from “org.hibernate.query” .Eclipse reference
Solution 3
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.
Related videos on Youtube
Chenya Zhang
Updated on July 09, 2022Comments
-
Chenya Zhang almost 2 years
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?
-
Xdg almost 8 yearsMaybe inspect given class in JavaDoc, there can be a suggestion - docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/query/… .
-
-
Chenya Zhang almost 8 yearsYes, I just tried. It also says the list() method is deprecate for Criteria as well... It seems that "all methods" are deprecate for both Query and Criteria, including uniqueResult() and others... I don't know why this could ever happen. I did exactly the same thing as all online tutorials.
-
Emiliano Dueñas almost 8 yearsThis method is too deprecated!
-
Johannes H over 7 yearsThis seems to be the same procedure as stated in my answer with the exception of an explicit cast isn't it ? Isn't it a runtime disadavantage to cast each object explicity (see stackoverflow.com/questions/2170872/…) ?
-
Pika over 7 years1. No. As I stated, The suppress warning is removed. 2. Yes, but I think it would be worth. It's usually worth trying to work out a way of avoiding the warning rather than suppressing it.
-
Menai Ala Eddine - Aladdin about 6 years
createCriteria()
is@Deprecated
in Hibernate 5.2. -
Menai Ala Eddine - Aladdin about 6 years
createQuery()
is not deprecated andgetResultList()
is not deprecated.see docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/query/….