altuure
Published in

altuure

altuure

Sep 21, 2010

1 min read

hibernate criteria API bug: unnecessary fetch

Although this is an important, it is a hidden bug unless you trace you generated SQL for hibernate.

simply the two lines of code below should generate same SQL

Criteria rootCriteria = session.createCriteria(OrderItem.class);
Criteria criteria = rootCriteria.createCriteria("order").createCriteria("customer");
List<OrderItem> list1 = criteria.add(Restrictions.eq("city", "city-1")).list();
List<OrderItem> list2 = session.createQuery("select s from OrderItem s where s.order.customer.city=?").setString(0,"city-1").list();
assertEquals(list1, list2);