【问题标题】:Hibernate and OSGi integration, it unable to load hibernate configuration fileHibernate和OSGi集成,无法加载hibernate配置文件
【发布时间】:2013-10-21 20:33:12
【问题描述】:

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

也许有人知道如何做到这一点的好例子?

目前我收到以下错误

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

提前谢谢你

【问题讨论】:

  • 您很可能需要使用 Hibernate 提供的最新版本,因为旧版本在类加载器和加载资源文件方面确实存在一些问题。
  • 我的问题已经解决,我只是用 3.2.6.ga 更新了 hibernate jar 版本,它就开始工作了。感谢 Achim Nierbeck 的回复和线索。

标签: hibernate osgi


【解决方案1】:

请按以下步骤操作:

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

  2. 创建项目 A 为 osgi 项目: (项目 A 将包含 Hibernate osgi loader, 和休眠库,没有你的实体类, 并且没有hibernate.cfg.xml) 提取 hibernate-release-4.2.8.Final.zip 到目录,将包源(org....)复制到 \hibernate-release-4.2.8.Final\project\hibernate-osgi\src\main\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',
    

设置捆绑类路径:(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     

==> 现在准备好项目 A(Osgi 项目)。

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

    在 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();
                }
    } 
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多