Hibernate 5.2 version -> A lot of Query methods deprecate?

久未见 提交于 2019-12-04 23:45:26
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.

Aniket Warey

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<>();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!