哪为朋友知道SessionFactory.java中session = sessionFactory.openSession()是干什么用的?老是这句出错!在线候着

chinarenx chinarenx 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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)

...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
chinarenx
Java EE 67,535 成员 225,843 内容