Persistence.xml 和 OSGi (Equinox)

发布于 2024-07-25 14:10:24 字数 5257 浏览 5 评论 0原文

我目前正在使用 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

我尝试将 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

    }

}

这里是我的目录结构如下:

替代文本 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"

HibernateBundle 包含所有 Hibernate 和 Persistence Jars。

这是

<?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>

我在清单的类路径中尝试过的 Persistence.xml 内容,但没有成功:

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

Bundle-类路径:., ../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

I am currently testing out using OSGi. I am running this through Eclipse. I want to have my DAO layer as part of an OSGi solution, but my first stumbling block is this error:

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

I have tried putting the persistence.xml file in a lot of different places, to no avail. Any ideas on what I am doing wrong?

Is there a way to manually load the persistence.xml?

The activator looks like this:

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

    }

}

Here is what my directory structure looks like:

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

Here is my 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"

HibernateBundle contains all of the Hibernate and Persistence Jars.

Here is my 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>

Things I have tried in the Manifest's Classpath with no luck:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

假情假意假温柔 2024-08-01 14:10:24

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

  • 您必须使用类加载器太多了...
    Thread.currentThread().setContextClassLoader(...)

  • 您可能会想要设置包类路径属性并手动添加依赖项,而不是安装 jar 包。

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

经过多次尝试后,上述所有努力可能都不起作用。

然而,使用 EclipseLink 就很简单了,该实现被设计为在 OSGI 环境中开箱即用,并且不存在任何类加载问题。

Use EclipseLink and forget about Hibernate and other implementations, because :

  • You'll have to play with the classloader too much...
    Thread.currentThread().setContextClassLoader(...)

  • You'll be tempted to set the bundle-classpath attribute and add dependencies manually instead of installing jar bundles.

  • You'll get provider not found errors or you might not be able to find persistence.xml

All the above efforts might not work after many attempts.

However, with EclipseLink it's a no brainer, the implementation was designed to work out of the box in an OSGI environment and there aren't any class loading headaches.

我三岁 2024-08-01 14:10:24
  1. (只是一个建议):如果您使用惰性加载程序而不是在激活器中完成这项工作会更好。 例如,使用调用到 SimpleDaoImpl 构造函数中的单例。
  2. 将 META-INF/persistent.xml 移动到 src 文件夹 (src/META-INF/persistent.xml) 下,因为在开发下 META-INF 文件夹不在类路径中,它仅在运行时模式下工作。
  3. 如果您使用 EclipseLink jpa OSGi,则您的 MANIFEST.MF 缺少 JPA-PersistenceUnits 条目。 添加

    JPA-PersistenceUnits:postgres

    进入 MANIFEST.MF。

  4. 然后在启动配置中设置 org.eclipse.persistence.jpa.osgi 的启动级别(对于 ecliselink 2.3.x,对于 2.1 则为 org.eclipse.persistence.jpa .x) 为 2,javax.persistence 的启动级别为 1。

祝你好运,实际上 2.3 在部署中存在问题,无法处理 bundleresource://xxxx URL :(,2.1.2 工作得很好;)

  1. (only a suggestion): Better if you use a lazy loader instead do the job into the activator. For example use a singleton that is invoked into SimpleDaoImpl contructor.
  2. Move META-INF/persistent.xml under src folder (src/META-INF/persistent.xml) because under develop META-INF folder is not in classpath, it works only in runtime mode.
  3. If you are using EclipseLink jpa OSGi, your MANIFEST.MF missing of JPA-PersistenceUnits entry. Add

    JPA-PersistenceUnits: postgres

    into the MANIFEST.MF.

  4. Then in your launch configuration set the start level of org.eclipse.persistence.jpa.osgi (for ecliselink 2.3.x otherwise org.eclipse.persistence.jpa for 2.1.x) to 2 and start level of javax.persistence to 1.

GOOD LUCK, actually 2.3 has a problem in deployment, doesn't handle bundleresource://xxxx URLs :(, 2.1.2 works very well ;)

梦冥 2024-08-01 14:10:24

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

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

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

private void initHibernate(BundleContext context) {
        try {
            final AnnotationConfiguration cfg = new AnnotationConfiguration();
            cfg.configure(context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml"));
            sessionFactory = cfg.buildSessionFactory();

        } catch (Exception e) {
            // TODO Auto-generated catch block
        }
    }

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

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

如您所见,我的 hibernate.cfg.xml 不在 META-INF 文件夹内。 它只是在 /src/ 下的根文件夹中......

希望有帮助。

克里斯托夫

I am not using persistence.xml but hibernate.cfg.xml which is similar:

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

In my Activator I am getting the file via the bundle context:
Here is some example code how I do it and also reference that file:>

private void initHibernate(BundleContext context) {
        try {
            final AnnotationConfiguration cfg = new AnnotationConfiguration();
            cfg.configure(context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml"));
            sessionFactory = cfg.buildSessionFactory();

        } catch (Exception e) {
            // TODO Auto-generated catch block
        }
    }

As you can see line which gets the config file is:

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

As you can see my hibernate.cfg.xml is NOT inside the META-INF folder. It is just in the root folder under /src/......

Hope that helps.

Christoph

许仙没带伞 2024-08-01 14:10:24

您需要在类路径上包含包含 META-INF 的目录。 在每个目录中搜索 META-INF,如果找到,则搜索 persistence.xml。

如果将“META-INF”放在类路径上,那么该目录中还需要另一个 META-INF。

You need to have the directory that contains META-INF on the classpath. Each directory is searched for META-INF and if found, then persistence.xml is searched for.

If you put "META-INF" on the classpath, then you'd need another META-INF in that directory.

断桥再见 2024-08-01 14:10:24

尝试在清单中使用 Bundle-ClassPath,如下所示

Bundle-ClassPath: ., /location/of/persistence.xml

Try using Bundle-ClassPath like this in your manifest

Bundle-ClassPath: ., /location/of/persistence.xml

枯寂 2024-08-01 14:10:24

Meta-inf 目录不在类路径上。 只需将其放在您的 src 目录下即可。 如果您希望它位于单独的位置,则必须指定 Bundle-Classpath 以包含该目录。 默认情况下,类路径是“.”。

The Meta-inf directory is not on the classpath. This should work by simply placing it under your src dirctory. If you want it in a separate location, then you will have to specify the Bundle-Classpath to include that directory. By default the classpath is '.'.

蓝眼泪 2024-08-01 14:10:24

我遇到了同样的问题。

我认为 eclipse link 是在 OSGi 环境中使用的最佳选择。 没有问题,因为您基本上将使用 JPA 实现。 当您需要迁移到 Hibernate 时,只需替换 persintece.xml 配置和一些库即可。

I'm getting the same problem.

I think eclipse link is the best option to use in a OSGi Environment. And there are no problem because you will work basically with the JPA implementation. When you need to move to Hibernate, just replace persintece.xml config and some libs.

全部不再 2024-08-01 14:10:24

您需要设置属性(对于休眠它将有所不同):

javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl

用于调用:

Persistence.createEntityManagerFactory(entityManagerFactoryName,properties)

以使其工作。

正如前面提到的,您需要类加载器包装。 您可以使用 https://issues.apache.org/jira/browse/OPENJPA 中的 ClassloaderEntityManager -1783 这样做。

问候

you need to set property (for hibernate it will be different):

javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl

for call:

Persistence.createEntityManagerFactory(entityManagerFactoryName, properties)

to make it work.

And as mentioned before, you need classloader wrapping. You can use ClassloaderEntityManager from https://issues.apache.org/jira/browse/OPENJPA-1783 to do that.

Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文