Retrieve value from Database using Session

public static void main(String[] args)
{
	UserDetails objUserDetail =  new UserDetails();
	.
	session.getTransaction().commit();

	objUserDetail = null;

	/*Retriving UserDetails from DB Using Session*/
	session = sessionFact.openSession();
	session.beginTransaction();
	objUserDetail = (UserDetails)session.get(UserDetails.class, 105);
	System.out.println(objUserDetail.getUserName());
}

To retrieve the value from session we use the primary key for the table in the above case.

 objUserDetail = (UserDetails)session.get(UserDetails.class, 105);
 System.out.println(objUserDetail.getUserName());

105 – is the UserId which is primary key for the USER_DETAILS table in DB.

Comments are closed.