-1

For example I have such query:

@SuppressWarnings("unchecked")
public List<MenuItems> listAllMenuItems() {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    List<MenuItems> menuItems = null;
    try {

        Criteria cr = session.createCriteria(MenuItems.class);
        menuItems = cr.list();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
    }
    session.getTransaction().commit();
    return menuItems;
}

Is there a way to avoid type safety warnings with Hibernate HQL results?

Thanks.

jose
  • 144
  • 1
  • 4
  • 16

2 Answers2

2

Use Collections.checkedList()

List<MenuItems> cats = Collections.checkedList(cr.list(), MenuItems.class);

Or use @suppressWarnings, which you have specified in your code

Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
1

I think it will solve your answer

List menuItems = Collections.checkedList(cr.list(), MenuItems.class);

for full details check this thread

How to avoid type safety warnings with Hibernate HQL results?

Community
  • 1
  • 1
Jaini Naveen
  • 155
  • 1
  • 17