In Java Persistence With Hibernate book (2007 edition), on page 390, its mentioned that "The same is true if a
query is executed through one of the Hibernate interfaces. Hibernate reads the result set of the query and marshals entity objects that are then returned to the application.
During this process, Hibernate interacts with the current persistence context."
However the above statement looks incorrect to me as :
Code:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Item i where i.itemId=1");
Iterator it = query.list().iterator();
while (it.hasNext ()){
Item i = (Item) it.next();
System.out.println(i.getItemDesc());
}
query = session.createQuery("from Item i where i.itemId=1");
it = query.list().iterator();
while (it.hasNext ()){
Item i = (Item) it.next();
System.out.println(i.getItemDesc());
}
tx.commit();
session.close();
In this example: SQL is executed 2 times...