您可以手动将 SessionFactory 添加到上下文中。虽然看起来很多代码,但实际上只有这 5 行代码。剩下的只是处理 InitialContext 似乎喜欢抛出的 NamingException。
更好的方法是使用 ContextListener 在启动期间自动添加会话
InitialContext initialContext = new InitialContext();
SessionFactory sf = (SessionFactory) initialContext.lookup("SessionFactory");
Configuration cfg = new Configuration();
cfg.configure();
sf = cfg.buildSessionFactory();
initialContext.bind("SessionFactory", sf);
这是完整的 Servlet goGet 方法
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Account acc;
InitialContext initialContext = null;
acc = new Account("asdf" + String.valueOf(new Date().getTime()), "asdf");
AccountHome home;
Transaction tx = null;
SessionFactory sf;
// Create an instance of the InitialContext
// So that we can lookup the SessionFactory property
// or add it if it does not yet exist
try {
initialContext = new InitialContext();
} catch (NamingException e) {
throw new ServletException("Unable to create InitalContext", e);
}
// Since SessionFactories are very expensive to create
// first attempt to lookup a cached instance of the SessionFactory
try {
sf = (SessionFactory) initialContext.lookup("SessionFactory");
} catch (NamingException e) {
// There is currently no session factory bound to this context
// Manually create it and bind it
Configuration cfg;
cfg = new Configuration();
cfg.configure();
sf = cfg.buildSessionFactory();
try {
initialContext.bind("SessionFactory", sf);
} catch (NamingException e1) {
throw new ServletException(
"Unable to bind the SessionFactory to the Inital Context");
}
}
// Start the transaction and perform work
tx = sf.getCurrentSession().beginTransaction();
try {
home = new AccountHome();
home.persist(acc);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw new ServletException("Work failed", e);
}
}
编辑:添加了 ContextListener
package ch.yaawi.platform;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionFactoryListener implements ServletContextListener {
private SessionFactory mSessionFactory;
public void contextDestroyed(ServletContextEvent event) {
if (mSessionFactory != null && !mSessionFactory.isClosed()) {
mSessionFactory.close();
}
}
public void contextInitialized(ServletContextEvent event) {
InitialContext initialContext = null;
try {
initialContext = new InitialContext();
} catch (NamingException e) {
throw new RuntimeException("Unable to create InitalContext", e);
}
try {
mSessionFactory = (SessionFactory) initialContext
.lookup("SessionFactory");
} catch (NamingException e) {
Configuration cfg;
cfg = new Configuration();
cfg.configure();
mSessionFactory = cfg.buildSessionFactory();
try {
initialContext.bind("SessionFactory", mSessionFactory);
} catch (NamingException e1) {
throw new RuntimeException(
"Unable to bind the SessionFactory to the Inital Context");
}
}
}
}
然后在你的 web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>ch.yaawi.platform.SessionFactoryListener</listener-class>
</listener>
</web-app>
导致将名称空间更改为您自己的名称空间。
希望这可以帮助某人