Retrieving records from Database in Hibernate
July 6, 2011 Leave a comment
In my previous post I have used example for the insertion of data in Hibernate. Here is another example for selecting the records from the database using hibernate. In the same project (from previous post), I have added this new class named UsersList.java and ShowUser.java without making any other changed.
package ps.com; //UsersList.java import org.hibernate.Criteria; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.hibernate.criterion.ProjectionList; import org.hibernate.criterion.Projections; import java.util.Iterator; import java.util.List; public class UsersList { public static void main(String[] args) { Session sess = null; try { SessionFactory sfact = new Configuration().configure().buildSessionFactory(); sess = sfact.openSession(); Criteria crit = sess.createCriteria(User.class); ProjectionList proList = Projections.projectionList(); proList.add(Projections.property("u_name")); proList.add(Projections.property("u_password")); crit.setProjection(proList); List list = crit.list(); Iterator it = list.iterator(); if (!it.hasNext()) { System.out.println("Data does not exist"); } else { System.out.println("User Name\t\tPassword"); System.out.println("----------------------------------"); while (it.hasNext()) { Object[] row = (Object[]) it.next(); for (int i = 0; i < row.length; i++) { System.out.print(row[i] +"\t\t\t"); } System.out.println(); } } sess.close(); } catch (Exception e) { // TODO: handle exception System.out.println("Error :" + e.getMessage()); } } }
Output :
Most of the time we need to select single row with condition, which can be achieved from this piece of code.
package ps.com; //ShowUser.java import java.util.Iterator; import java.util.List; import org.hibernate.Criteria; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.hibernate.criterion.Example; import org.hibernate.criterion.ProjectionList; import org.hibernate.criterion.Projections; public class ShowUser { public static void main(String[] args) { Session sess = null; try { SessionFactory sfact = new Configuration().configure() .buildSessionFactory(); sess = sfact.openSession(); User user = new User(); user.setU_name("sharma"); Example example = Example.create(user); Criteria crit = sess.createCriteria(User.class).add(example); ProjectionList proList = Projections.projectionList(); proList.add(Projections.property("u_name")); proList.add(Projections.property("u_password")); crit.setProjection(proList); List list = crit.list(); Iterator it = list.iterator(); System.out.println("User Name\t\tPassword"); System.out.println("----------------------------------"); Object[] row = (Object[]) it.next(); for (int i = 0; i < row.length; i++) { System.out.print(row[i] + "\t\t\t"); } sess.close(); } catch (Exception e) { // TODO: handle exception System.out.println("Error :" + e.getMessage()); } } }
Note: In this code, Example class that is used to add criteria is defined in imported package org.hibernate.criterion.
Recent Comments