17

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?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Chenya Zhang
  • 463
  • 3
  • 11
  • 22
  • Maybe inspect given class in JavaDoc, there can be a suggestion - http://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/query/QueryProducer.html#createSQLQuery-java.lang.String- . – Xdg Aug 18 '16 at 07:46

6 Answers6

16
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.

Pika
  • 507
  • 1
  • 6
  • 16
  • This 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 http://stackoverflow.com/questions/2170872/does-java-casting-introduce-overhead-why) ? – Johannes H Oct 16 '16 at 21:59
  • 1. 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. – Pika Oct 17 '16 at 06:22
15

Don't import QUERY from org.hibernate (As its Deprecated now). Instead import from “org.hibernate.query” .Eclipse reference

A W
  • 1,041
  • 11
  • 18
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.

Johannes H
  • 376
  • 2
  • 10
0

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;
}
A. Horst
  • 78
  • 8
  • Yes, 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. – Chenya Zhang Jul 19 '16 at 14:43
  • `createCriteria()` is `@Deprecated` in Hibernate 5.2. – Menai Ala Eddine - Aladdin Mar 30 '18 at 17:33
0

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.

0

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<>();
    }
}
sharmaap
  • 66
  • 6