java – 以编程方式配置嵌入式tomcat 7的hibernate
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 以编程方式配置嵌入式tomcat 7的hibernate,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3303字,纯文字阅读大概需要5分钟。
内容图文
我试图在我的应用程序中配置嵌入式tomcat实例,没有任何配置文件.
我做了一些研究,基于这个long tutorial和a shorter one我提取了这个步骤:
>创建一个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从链接数据库创建和检索数据,对吧? (现在我不确定,究竟是怎么回事,但这不是重点)
但不幸的是,当我试图将监听器添加到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(没有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全部内容,希望文章能够帮你解决java – 以编程方式配置嵌入式tomcat 7的hibernate所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。