Persistence.xml と OSGi (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
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
これが私のものです マニフェスト.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>
マニフェストのクラスパスで試してみましたがうまくいきませんでした:
バンドルクラスパス:.、META-INF/persistence.xml
バンドルクラスパス:., ../META-INF/persistence.xml
バンドルクラスパス:., /META-INF/persistence.xml
バンドルクラスパス:., ./META-INF/persistence.xml
バンドルクラスパス:.、META-INF
バンドルクラスパス:., ../META-INF
バンドルクラスパス:., /META-INF
バンドルクラスパス:., ./META-INF
バンドルクラスパス:., C:\Workspaces\OSGiJPA\Dao\META-INF\persistence.xml
バンドルクラスパス:., C:\Workspaces\OSGiJPA\Dao\META-INF
解決
私はpersistence.xmlではなく、同様のhibernate.cfg.xmlを使用しています。
src/main/resource/hibernate/hibernate.cfg.xml
私のアクティベーターでは、バンドルコンテキストを介してファイルを取得しています。これを行う方法とそのファイルを参照するコード例を次に示します。>
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/...... の下のルート フォルダーにあります。
それが役立つことを願っています。
クリストフ
他のヒント
使用 エクリプスリンク Hibernate やその他の実装のことは忘れてください。理由は次のとおりです。
クラスローダーをいじりすぎなければなりません...Thread.currentThread().setContextClassLoader(...)
jar バンドルをインストールする代わりに、bundle-classpath 属性を設定して依存関係を手動で追加したくなるでしょう。
もらえるよ プロバイダーが見つかりません エラーが発生するか、見つからない可能性があります 永続性.xml
上記のすべての取り組みは、何度試してもうまくいかない可能性があります。
ただし、EclipseLink を使用すると、それは簡単で、実装は OSGI 環境ですぐに機能するように設計されており、クラス読み込みの問題はありません。
- (単なる提案):アクティベーターでジョブを実行する代わりに、遅延ローダーを使用する方が良いでしょう。たとえば、SimpleDaoImpl コンストラクターで呼び出されるシングルトンを使用します。
- 開発中の META-INF フォルダーはクラスパスになく、ランタイム モードでのみ動作するため、META-INF/persistent.xml を src フォルダー (src/META-INF/persistent.xml) の下に移動します。
EclipseLink jpa OSGi を使用している場合、MANIFEST.MF に JPA-PersistenceUnits エントリがありません。追加
JPA-PersistenceUnits:ポストグレ
MANIFEST.MF にコピーします。
- 次に、起動構成で org.eclipse.persistence.jpa.osgi (ecliselink 2.3.x の場合は org.eclipse.persistence.jpa (2.1.x の場合)) の開始レベルを 2 に設定し、javax.persistence の開始レベルを 1 に設定します。
幸運を祈ります。実際、2.3 にはデプロイメントに問題があり、bundleresource://xxxx URL を処理できません :(、2.1.2 は非常にうまく機能します ;)
クラスパス上に META-INF を含むディレクトリが必要です。各ディレクトリで META-INF が検索され、見つかった場合は、persistence.xml が検索されます。
クラスパスに「META-INF」を置く場合は、そのディレクトリに別の META-INF が必要になります。
マニフェストで次のように Bundle-ClassPath を使用してみてください
バンドルクラスパス:., /location/of/persistence.xml
Meta-inf ディレクトリがクラスパス上にありません。これは、単に下に置くだけで機能するはずです。 送信元 ディレクトリ。別の場所に置きたい場合は、そのディレクトリを含めるバンドル クラスパスを指定する必要があります。デフォルトでは、クラスパスは「.」です。
同じ問題が発生しています。
Eclipse Link は OSGi 環境で使用するのに最適なオプションだと思います。基本的にはJPA実装で作業するので問題ありません。Hibernate に移行する必要がある場合は、persintece.xml 設定といくつかのライブラリを置き換えるだけです。
プロパティを設定する必要があります(休止状態の場合は異なります)。
javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl
電話の場合:
Persistence.createEntityManagerFactory(entityManagerFactoryName, プロパティ)
それを機能させるために。
そして、前に述べたように、クラスローダーのラッピングが必要です。ClassloaderEntityManager は次から使用できます。 https://issues.apache.org/jira/browse/OPENJPA-1783 それをするために。
よろしく