Next, we will retrieve the data stored in the database table. Create a query to select data from the table. The query is defined in Hibernate Query Language (HQL) syntax which is similar to SQL.
String hqlQuery ="from example.hibernate.Catalog";
If the Select
clause is not specified in the query all of the fields selected in the From
clause are selected from the mapped class. The From
clause is specified with the mapped Java class, example.hibernate.Catalog
, not the database table. The Java class object to database mapping is performed by the mapping file and the properties file. Open a Session
object from the SessionFactory
object using the openSession()
method:
Session sess = sessionFactory.openSession();
Create a Query
object with the createQuery(hqlQuery)
method of the Session
object:
Query query = sess.createQuery(hqlQuery);
Obtain a List
from the HQL query with the list()
method of the Query
object:
List list = query.list();
Iterate over the...