哪为朋友知道SessionFactory.java中session = sessionFactory.openSession()是干什么用的?老是这句出错!在线候着
public class SessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static final Configuration cfg = new Configuration();
private static net.sf.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession(); //就是这句!
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private SessionFactory() {
}
}
2个例子都出现这个错,我用的MYECLIPSE 4.2,Hibernate 2。session = sessionFactory.openSession();这句有什么玄机吗?
java.lang.NullPointerException
com.nscorp.hibernate.SessionFactory.currentSession(SessionFactory.java:31)
com.nscorp.hibernate.VipService.getVipdataList(VipService.java:200)
org.apache.jsp.AddVipData_jsp._jspService(AddVipData_jsp.java:120)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)