Persistence.xml和OSGi(Equinox)

mai*_*rgs 6 java osgi jpa java-ee equinox

我目前正在测试使用OSGi.我通过Eclipse运行它.我希望将我的DAO层作为OSGi解决方案的一部分,但我的第一个绊脚石是这个错误:

Jun 29, 2009 6:12:37 PM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.0.GA
Jun 29, 2009 6:12:37 PM org.hibernate.ejb.Version <clinit>
INFO: Hibernate EntityManager 3.3.0.GA
Jun 29, 2009 6:12:37 PM org.hibernate.ejb.Ejb3Configuration configure
INFO: Could not find any META-INF/persistence.xml file in the classpath
Run Code Online (Sandbox Code Playgroud)

我已经尝试将persistence.xml文件放在很多不同的地方,但无济于事. 关于我做错了什么的任何想法?

有没有办法手动加载persistence.xml?

激活剂是这样的:

package com.activator;


public class PersistenceActivator implements BundleActivator {

    @Override
    public void start(BundleContext arg0) throws Exception {

        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("postgres");
        EntityManager em = emf.createEntityManager();

        SimpleDaoImpl dao = new SimpleDaoImpl();
        dao.setEntityManager(em);

    }

    @Override
    public void stop(BundleContext arg0) throws Exception {
        // TODO Auto-generated method stub

    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的目录结构:

alt text http://www.freeimagehosting.net/uploads/7b7b7d2d30.jpg

这是我的Manifest.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Dao Plug-in
Bundle-SymbolicName: Dao
Bundle-Version: 1.0.0
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.4.0"
Bundle-Activator: com.activator.PersistenceActivator
Export-Package: com.dao.service
Require-Bundle: HibernateBundle;bundle-version="1.0.0"
Run Code Online (Sandbox Code Playgroud)

HibernateBundle包含所有Hibernate和Persistence Jars.

这是我的Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence>

    <!-- Sample persistence using PostgreSQL. See postgres.txt. -->
    <persistence-unit name="postgres" transaction-type="RESOURCE_LOCAL">

        <properties>


            <property name="hibernate.archive.autodetection" value="class" />

            <!--
                Comment out if schema exists & you don't want the tables dropped.
            -->
            <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <!-- drop/create tables @startup, drop tables @shutdown -->


            <!-- Database Connection Settings -->
            <property name="hibernate.connection.autocommit">true</property>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="postgres" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test" />

            <!-- Not sure about these...  -->
            <property name="hibernate.max_fetch_depth">16</property>
            <property name="hibernate.jdbc.batch_size">1000</property>
            <property name="hibernate.use_outer_join">true</property>
            <property name="hibernate.default_batch_fetch_size">500</property>

            <!-- Hibernate Query Language (HQL) parser. -->
            <property name="hibernate.query.factory_class">
                org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>

            <!-- Echo all executed SQL to stdout -->
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.format_sql">false</property>

            <!-- Use c3p0 for the JDBC connection pool -->
            <property name="hibernate.c3p0.min_size">3</property>
            <property name="hibernate.c3p0.max_size">100</property>
            <property name="hibernate.c3p0.max_statements">100</property>

            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />

        </properties>
    </persistence-unit>



</persistence>
Run Code Online (Sandbox Code Playgroud)

我在Manifest的Classpath中尝试过没有运气的事情:

Bundle-ClassPath:.,META-INF/persistence.xml

Bundle-ClassPath:.,../META-INF/Persistence.xml

Bundle-ClassPath:.,/ META-INF/Persistence.xml

Bundle-ClassPath:.,./ META-INF/Persistence.xml

Bundle-ClassPath:.,META-INF

Bundle-ClassPath:.,../META-INF

Bundle-ClassPath:.,/ META-INF

Bundle-ClassPath:.,./ META-INF

Bundle-ClassPath:.,C:\ Workspaces\OSGiJPA\Dao\META-INF\persistence.xml

Bundle-ClassPath:.,C:\ Workspaces\OSGiJPA\Dao\META-INF

小智 6

使用EclipseLink并忘记Hibernate和其他实现,因为:

  • 你将不得不太多地使用类加载器... Thread.currentThread().setContextClassLoader(...)

  • 您将很想设置bundle-classpath属性并手动添加依赖项而不是安装jar包.

  • 您将获得提供程序未找到错误,或者您可能无法找到persistence.xml

经过多次尝试,上述所有努力都可能无效.

但是,使用EclipseLink并不是一件容易的事,实现的目的是在OSGI环境中开箱即用,并且没有任何类加载问题.

  • EclipseLink确实是OSGi的更好选择(顺便说一下......它的JP为JPA 2).根据EL UserGuid,你应该加载EMFactory:HashMap <String,Object> properties = new HashMap <String,Object>(); properties.put(PersistenceUnitProperties.CLASSLOADER,this.getClass().getClassLoader()); entityManagerFactory = new PersistenceProvider().createEntityManagerFactory(PU_NAME,properties); (3认同)

Chr*_*oph 1

我没有使用 persistence.xml 而是使用 hibernate.cfg.xml ,它是类似的:

src/main/resource/hibernate/hibernate.cfg.xml

在我的激活器中,我通过捆绑上下文获取文件:以下是一些示例代码,说明我如何执行此操作并引用该文件:>

私有无效 initHibernate(BundleContext 上下文) {
        尝试 {
            最终 AnnotationConfiguration cfg = new AnnotationConfiguration();
            cfg.configure(context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml"));
            sessionFactory = cfg.buildSessionFactory();

        } catch (异常 e) {
            // TODO 自动生成的 catch 块
        }
    }

正如您所看到的,获取配置文件的行是:

context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml")

正如您所看到的,我的 hibernate.cfg.xml 不在 META-INF 文件夹内。它就在/src/......下的根文件夹中

希望有帮助。

克里斯托夫