Hibernate与OSGi集成,它无法加载hibernate配置文件

时间:2013-10-14 09:46:22

标签: hibernate osgi

我正在创建OSGi动态包,它应该与数据库通信,我正在使用Hibernate,因为我在非OSGI应用程序中使用它。 我在OSGi项目的lib目录中放置了原始的hibernate jar,并确保这些jar在项目的构建路径和运行时类路径上,我在OSGi捆绑jar的根目录中复制了所有配置文件(即hibernate-cfg.xml) 。 当我在OSGi容器中执行我的bundle(JAR)时,它会因为没有找到hibernate-cfg.xml文件而抛出错误。

也许有人知道怎么做的好例子?

目前我收到了以下错误

2013-10-14 14:56:10错误HibernateUtil:41 - SessionFactory创建失败:org.hibernate.HibernateException:/hibernate.cfg.xml not found

提前谢谢

1 个答案:

答案 0 :(得分:0)

请步骤:

  1. 在hibernate.org(现在的Hibernate4.3.0Final)下载Hibernate zip文件,   但我用Hibernate 4.2.8Final

  2. 测试了Ok
  3. 将项目A创建为osgi项目:     (项目A将包含Hibernate osgi加载器,       和hibernate Library,没有你的Entities类,       并且没有hibernate.cfg.xml)      提取hibernate-release-4.2.8.Final.zip      到目录,复制包源(org ....)       \ hibernate-release-4.2.8.Final \项目\休眠OSGi的的\ src \主\ java中      进入项目A的src。

     Setup Activator class:  (MANIFEST.MF)
      Bundle-Activator: org.hibernate.osgi.HibernateBundleActivator
    
     Create directory 'libs/hiber-4.2.8' in project A:
     Copy All <DIR>\hibernate-release-4.2.8.Final\lib to your 'libs/hiber-4.2.8',
    
  4. 设置Bundle Classpath:(MANIFEST.MF)

        Bundle-ClassPath: .,
         libs/hiber-4.2.8/jpa/hibernate-entitymanager-4.2.8.Final.jar,
         libs/hiber-4.2.8/optional/ehcache/ehcache-core-2.4.3.jar,
         libs/hiber-4.2.8/optional/ehcache/slf4j-api-1.6.1.jar,
         libs/hiber-4.2.8/required/antlr-2.7.7.jar,
         libs/hiber-4.2.8/required/dom4j-1.6.1.jar,
         libs/hiber-4.2.8/required/hibernate-commons-annotations-4.0.2.Final.jar,
         libs/hiber-4.2.8/required/hibernate-core-4.2.8.Final.jar,
         libs/hiber-4.2.8/required/hibernate-jpa-2.0-api-1.0.1.Final.jar,
         libs/hiber-4.2.8/required/javassist-3.18.1-GA.jar,
         libs/hiber-4.2.8/required/jboss-logging-3.1.0.GA.jar,
         libs/hiber-4.2.8/required/jboss-transaction-api_1.1_spec-1.0.1.Final.jar
    

    设置导出包:

    Export-Package: javax.persistence,
    org.hibernate,
    org.hibernate.annotations,
    org.hibernate.cfg,
    org.hibernate.criterion,
    org.hibernate.dialect,
    org.hibernate.dialect.function,
    org.hibernate.exception,
    org.hibernate.internal.util,
    org.hibernate.jdbc,
    org.hibernate.mapping,
    org.hibernate.osgi,
    org.hibernate.property,
    org.hibernate.service,
    org.hibernate.tool.hbm2ddl,
    org.hibernate.type     
    

    ==&GT;现在准备好项目A(Osgi项目)。

    1. 在项目B中创建具有您的实体类的Osgi项目B,      文件hibernate.cfg.xml,Jdbc驱动程序库可以在B osgi中声明。

      在B激活器中:

      public void start(BundleContext bundleContext) throws Exception {
          Activator.context = bundleContext;
          this.loadOsgiHibernateService(bundleContext);
      }
      
      private void loadOsgiHibernateService(BundleContext bundleContext) {
      
          ServiceReference<?> ref = context
                  .getServiceReference(SessionFactory.class.getName());
          if (ref != null) {
              SessionFactory factory = (SessionFactory) context.getService(ref);
                      // Ready session factory. 
                      Session session= factory.getCurrentSession();
                  }
      }