Hibernate 5.2 version -> A lot of Query methods deprecate?
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.
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.