6

現在、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 Jar が含まれています。

ここに私の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>

マニフェストのクラスパスで試したことは運が悪い:

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

バンドル クラスパス: ., ../META-INF

Bundle-ClassPath: ., /META-INF

バンドル クラスパス: ., ./META-INF

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

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

4

8 に答える 8

6

EclipseLinkを使用し、Hibernate やその他の実装については忘れてください。次の理由によります。

  • クラスローダーをいじる必要がありすぎます... Thread.currentThread().setContextClassLoader (...)

  • jar バンドルをインストールする代わりに、bundle-classpath 属性を設定して手動で依存関係を追加したくなるでしょう。

  • プロバイダーが見つからないというエラーが発生するか、 persistence.xmlが見つからない可能性があります。

上記のすべての取り組みは、何度も試行してもうまくいかない場合があります。

ただし、EclipseLink を使用することは非常に簡単です。実装は OSGI 環境ですぐに動作するように設計されており、クラスのロードに関する頭痛の種はありません。

于 2009-07-22T01:19:05.167 に答える
2
  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 (ecriselink 2.3.x の場合、2.1.x の場合は org.eclipse.persistence.jpa) の開始レベルを 2 に設定し、javax.persistence の開始レベルを 1 に設定します。

幸運を祈ります、実際には 2.3 には展開に問題があり、bundleresource://xxxx URL を処理しません :(, 2.1.2 は非常にうまく機能します ;)

于 2011-10-13T14:46:25.833 に答える
1

私はpersistence.xmlを使用していませんが、似ているhibernate.cfg.xmlを使用しています:

ソース/メイン/リソース/休止状態/休止状態.cfg.xml

私の Activator では、バンドル コンテキストを介してファイルを取得しています: これは、私がそれを行う方法とそのファイルを参照するコードの例です:>

private void initHibernate(BundleContext context) {
        試す {
            最終 AnnotationConfiguration cfg = 新しい AnnotationConfiguration();
            cfg.configure(context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml"));
            sessionFactory = cfg.buildSessionFactory();

        キャッチ(例外e){
            // TODO 自動生成された catch ブロック
        }
    }

ご覧のとおり、構成ファイルを取得する行は次のとおりです。

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

ご覧のとおり、私の hibernate.cfg.xml は META-INF フォルダー内にはありません。/src/の下のルートフォルダーにあります......

それが役立つことを願っています。

クリストフ

于 2009-07-09T15:47:56.690 に答える
1

クラスパスに META-INF を含むディレクトリが必要です。各ディレクトリで META-INF が検索され、見つかった場合は persistence.xml が検索されます。

クラスパスに「META-INF」を配置すると、そのディレクトリに別の META-INF が必要になります。

于 2010-03-09T16:34:31.920 に答える
0

マニフェストでこのようなBundle-ClassPathを使用してみてください

Bundle-ClassPath:。、/location/of/persistence.xml

于 2009-06-30T06:02:35.477 に答える
0

プロパティを設定する必要があります(休止状態の場合は異なります):

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

電話の場合:

Persistence.createEntityManagerFactory(entityManagerFactoryName, プロパティ)

それを機能させるために。

前述のとおり、クラスローダーのラッピングが必要です。これを行うには、 https: //issues.apache.org/jira/browse/OPENJPA-1783 の ClassloaderEntityManager を使用できます。

よろしく

于 2010-09-02T10:01:10.553 に答える
0

Meta-inf ディレクトリがクラスパスにありません。これは、srcディレクトリの下に配置するだけで機能するはずです。別の場所に配置する場合は、Bundle-Classpath を指定してそのディレクトリを含める必要があります。デフォルトでは、クラスパスは「.」です。

于 2009-06-30T13:06:09.147 に答える
0

私は同じ問題を抱えています。

OSGi 環境で使用するには、eclipse リンクが最適なオプションだと思います。基本的にJPAの実装で作業するので問題ありません。Hibernate に移行する必要がある場合は、persintece.xml 構成といくつかのライブラリを置き換えるだけです。

于 2010-07-28T14:22:10.183 に答える