View RSS Feed

My Java Tips

Using the hibernate objects

Rate this Entry
by , 11-06-2011 at 05:18 PM (805 Views)
I assume that you know how to create hibernate archive and you have created a fully configured SessionFactory for use in other parts of your application. I will present an simple JSP which creates a Session from the SessionFactory and issues a query directly.




Itís a good practice to put the Hibernate access code in a servlet or in a
session bean, but for example purposes I will use JSP.

The example shows how to access Hibernate session in the JSP.

Java Code:
InitialContext ctx = new InitialContext();
SessionFactory factory = (SessionFactory)
ctx.lookup("java:/hibernate/SessionFactory");
Session hsession = factory.openSession();
try {
Query query = hsession.createQuery("from org.jboss.appl.Player order by name");
request.setAttribute("players", query.list());
} finally {
hsession.close();
}

Submit "Using the hibernate objects" to Facebook Submit "Using the hibernate objects" to Digg Submit "Using the hibernate objects" to del.icio.us Submit "Using the hibernate objects" to StumbleUpon Submit "Using the hibernate objects" to Google

Tags: None Add / Edit Tags
Categories
Hibernate

Comments