Hibernate和OSGi集成,无法加载Hibernate配置文件

Hibernate和OSGi集成,无法加载Hibernate配置文件,hibernate,osgi,Hibernate,Osgi,我正在创建OSGi动态捆绑包,它应该与数据库通信,我在非OSGi应用程序中使用Hibernate。 我将原始的hibernate jar放入OSGi项目的lib dir中,并确保这些jar位于项目的构建路径和运行时类路径上,我复制了OSGi绑定jar根目录中的所有配置文件(即hibernate cfg.xml)。 当我在OSGi容器中执行包(JAR)时,它会因为找不到hibernate-cfg.xml文件而抛出错误 也许有人知道如何做到这一点 目前,我得到以下错误 2013-10-14 14:5

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

也许有人知道如何做到这一点

目前,我得到以下错误

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

提前感谢

请执行以下步骤:

  • 在Hibernate.org(现在是Hibernate4.3.0Final)下载Hibernate zip文件, 但我用Hibernate4.2.8测试还行

  • 将项目A创建为osgi项目: (项目A将包含Hibernate osgi加载程序, 和hibernate库,没有实体类, 并且没有hibernate.cfg.xml) 解压缩hibernate-release-4.2.8.Final.zip 将包源(组织…)复制到目录中 \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',
    
    设置导出包:

    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项目)

  • 创建Osgi项目B,在项目B中使用实体类, 文件hibernate.cfg.xml,您可以在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();
                }
    } 
    
    public void start(BundleContext BundleContext)引发异常{
    Activator.context=bundleContext;
    这个.loadOsgiHibernateService(bundleContext);
    }
    私有void loadOsgiHibernateService(BundleContext BundleContext){
    ServiceReference ref=上下文
    .getServiceReference(SessionFactory.class.getName());
    如果(ref!=null){
    SessionFactory=(SessionFactory)context.getService(ref);
    //就绪会话工厂。
    Session Session=factory.getCurrentSession();
    }
    } 
    

  • 您很可能需要使用Hibernate提供的最新版本,因为旧版本在类加载器和加载资源文件方面确实存在一些问题。我的问题已经解决,我只是用3.2.6.ga更新了Hibernate jar版本,它就开始工作了。感谢阿希姆·尼尔贝克的回复和提示。
    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();
                }
    }