第四个文件HibernateUtiljava
package hb;
import netsfhibernate*;
import netsfhibernatecfg*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration()configure()buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException(Exception building SessionFactory:
+ exgetMessage() ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
// Session s;
Session s = (Session) sessionget();
// Open a new Session if this Thread has none yet
if (s == null) {
s = sessionFactoryopenSession();
sessionset(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) sessionget();
sessionset(null);
if (s != null)
sclose();
}
}
第五个文件 addCatjsp
<%@ page contentType=text/html; charset=GB %>
<%@ page import=hb* netsfhibernate* %>
<html>
<head>
<title>test</title>
</head>
<body bgcolor=#ffffff]
<h>Test Hibernate</h>
<a href=getCatsjsp]View Cats<br>
<%
if(requestgetParameter(Go) != null) {
String name = requestgetParameter(name);
if(name == null || namelength() < ) {
outprintln(Name can not be empty!);
return;
}
SessionFactory sessionFactory;
netsfhibernateSession hsession = HibernateUtilcurrentSession();
Transaction tx = hsessionbeginTransaction();
[] [] [] []