我正在尝试在我的应用程序中配置嵌入式 tomcat 实例,而无需任何配置文件。
我做了一些研究并以此为基础长教程 http://www.ehow.com/how_7999843_stepbystep-hibernate-tomcat-mysql-tutorial.html and 较短的一个 https://community.jboss.org/wiki/UsingHibernateWithTomcat我提取了这个步骤:
-
创建一个ServletContextListener
@WebListener //some articles on the web mentioned, that this would add the
//Listener automatically to the app context, but i cant believe that this works in my case
public class HibernateListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
HibernateUtil.getSessionFactory(); // create a factory
}
public void contextDestroyed(ServletContextEvent event) {
HibernateUtil.getSessionFactory().close(); // free resources
}
}
-
将该侦听器添加到应用程序上下文中
Context rootCtx = tomcat.addContext("", base.getAbsolutePath());
rootCtx.getServletContext().addListener("com.example.listeners.HibernateListener");
tomcat.start();
tomcat.getServer().await();
-
实施HibernateUtil
具有必要配置的类
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
//should i call .configure() on the returned Configuration here?
sessionFactory = getConfiguration()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
private static Configuration getConfiguration(){
Configuration c = new Configuration();
c.setProperty("hibernate.connection.url", "jdbc:hsqldb:hsql://localhost:1234/mydb1");
c.setProperty("hibernate.connection.username", "SA");
c.setProperty("hibernate.connection.password", "");
c.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
c.setProperty("dialect", "org.hibernate.dialect.HSQLDialect");
c.setProperty("cache.provider_class", "org.hibernate.cache.NoCacheProvider");
c.setProperty("cache.use_query_cache", "false");
c.setProperty("cache.use_minimal_puts", "false");
c.setProperty("max_fetch_depth", "3");
c.setProperty("show_sql", "true");
c.setProperty("format_sql", "true");
c.setProperty("hbm2ddl.auto", "create");
c.addPackage("com.example.models");
c.addAnnotatedClass(MyClass.class);
return c;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
现在我应该以某种方式与MyClass
通过 hibernate 创建和检索链接数据库中的数据,对吗? (现在我不确定,具体如何,但这不是重点)
但不幸的是我得到了NullPointerException
当我尝试将侦听器添加到 tomcat 时
线程“main”中的异常 java.lang.NullPointerException
org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1278)
在
org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:649)
指向该线rootCtx.getServletContext().addListener("com.example.listeners.HibernateListener");
EDIT 1
但如果我正在跑步休眠独立(没有tomcat)它可以工作fine。数据正在保存,没有错误!
In HibernateUtil
public static void main(String[] args) {
MyClass mycls = new MyClass();
mycls.setMyProperty("My Property");
Session session = getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
session.save(mycls);
transaction.commit();
}
所以我认为我配置休眠的方式很好。该错误与侦听器添加有关...
我在这里做错了什么?
经过深入研究 Tomcat 的源代码,我找到了一种可能的解决方案:
rootCtx.addApplicationListener(new ApplicationListener("com.example.listeners.HibernateListener", false));
它满足了我的需要!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)