[英]configure hibernate with embedded tomcat 7 programmatically
我試圖在我的應用程序中配置嵌入式tomcat實例,沒有任何配置文件。
我做了一些研究,並基於這個長篇教程和一個較短的一個我提取了這個步驟:
創建一個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 } }
將該Listener添加到應用程序上下文中
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從鏈接數據庫創建和檢索數據,對吧? (現在我不確定,究竟是怎么回事,但這不是重點)
但不幸的是,當我試圖將監聽器添加到tomcat時,我得到一個NullPointerException
org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1278)中org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:649)中線程“main”java.lang.NullPointerException中的異常
它指向行rootCtx.getServletContext().addListener("com.example.listeners.HibernateListener");
編輯1
但是,如果我正在運行獨立的hibernate (沒有tomcat),它可以正常工作 。 正在保存數據而不會出錯!
在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();
}
所以我認為我配置hibernate的方式很好。 該錯誤與偵聽器添加有關...
我在這做錯了什么?
在深入挖掘Tomcat的源代碼后,我找到了一個可能的解決方案:
rootCtx.addApplicationListener(new ApplicationListener("com.example.listeners.HibernateListener", false));
它做我需要的!
我認為你在初始化之后錯過了配置c的方法cinfigure()的調用。
所以看起來應該是這樣::
SessionFactory sessionFactory = getConfiguration().confgure().buildSessionFactory();
要么
c = getConfiguration();
sessionFactory = c.configure().buildSessionFactory();
UPDATE
為了在runtim添加完整屬性,您可以創建一個Prperties對象並使用該方法。
configuration.buildSettings(Properties props)
我不確定但是使用這種方法可能是因為hibernate不會在類路徑中查找hibernate.cfg.xml或.properties。
UPDATE
您還可以嘗試在getSessionFactory方法中調用getConfiguration方法,而不是在HibernateUtil expliciteLy的靜態初始化程序中調用它
public static getSessionFactory() {
Configuration c = getConfiguration();
sessionFactory = c.buildSessionFactory();
return sessionFactory;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.