gpt4 book ai didi

java - 以编程方式使用嵌入式 tomcat 7 配置 hibernate

转载 作者:搜寻专家 更新时间:2023-11-01 02:46:26 27 4
gpt4 key购买 nike

我正在尝试在没有任何配置文件的情况下在我的应用程序中配置一个嵌入式 tomcat 实例。

我做了一些研究并以此为基础 long tutoriala shorter one我提取了这个步骤:

  1. 创建一个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
    }
    }
  2. 将该监听器添加到应用上下文

    Context rootCtx = tomcat.addContext("", base.getAbsolutePath());
    rootCtx.getServletContext().addListener("com.example.listeners.HibernateListener");

    tomcat.start();
    tomcat.getServer().await();
  3. 使用必要的配置实现 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;
    }
    }
  4. 现在我应该以某种方式使用 MyClass 通过 hibernate 从链接数据库创建和检索数据,对吧? (现在我不确定,具体如何,但这不是重点)

但不幸的是,当我尝试将监听器添加到 tomcat 时,出现了 NullPointerException

Exception in thread "main" java.lang.NullPointerException at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1278) at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:649)

它指向行 rootCtx.getServletContext().addListener("com.example.listeners.HibernateListener");

编辑 1

但如果我正在运行hibernate standalone(没有 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));

它满足了我的需要!

关于java - 以编程方式使用嵌入式 tomcat 7 配置 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872943/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com