2013-07-30 2 views
11

JPA @Entityアノテーションを利用して、クラスエンティティにJ2SEのpersistence.xmlファイルを宣言しないことを望みます。私は避けたいのですが:ここpersistence.xmlファイルで永続クラスを宣言しないようにJPAエンティティをスキャンする方法はありますか?

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class>com.mycompany.entities.Class1</class> 
    <class>com.mycompany.entities.Class2</class> 
    <class>com.mycompany.entities.Class3</class> 
</persistence-unit> 

と私の実際のpersistence.xmlが

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <properties> 
     <!-- Scan for annotated classes and Hibernate mapping XML files --> 
     <property name="hibernate.archive.autodetection" value="class, hbm" /> 
     <property name="hibernate.cache.use_second_level_cache" value="false" /> 
     <property name="hibernate.cache.use_query_cache" value="false" /> 
     <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 
    </properties> 
</persistence-unit> 

内からpersistence.xmlファイルでJPAエンティティをスキャンする標準的な方法があります似て何をされますJARモジュール? JARモジュール内のpersistence.xmlファイル内のJPAエンティティをスキャンするための非標準のHibernateの方法はありますか?

答えて

14

- あなたのエンティティとpersistence.xmlが、あなたのjarを構築するときに同じクラスパスになるようにしてください。

エンティティは、別のクラスパスに置かれているとスキャンできません。別のクラスパスに座らせる必要がある場合は、私がそれを動作させるために見た1つのトリックを得ます:No autodetection of JPA Entities in maven-verify

終了する場所がわからない場合は、.jarファイルを解凍してピークにすることができます。これは、アンパック永続Webプロジェクトです:

Unpacked Web Persistence JAR

は、COMディレクトリ下にある私のクラスに注意してください、そして私のpersistence.xmlは、META-INFディレクトリにあります。どちらも同じ 'クラス'クラスパスにあるので、自動スキャンが機能します。

-hibernate.archive.autodetectionプロパティを設定します。

<!-- Scan for annotated classes and Hibernate mapping XML files --> 
<property name="hibernate.archive.autodetection" value="class, hbm" /> 
永続性ユニットに偽

-Add

<persistence-unit name=...> 
<exclude-unlisted-classes>false</exclude-unlisted-classes> 
    ... 

うまくいけば、それらの一つは、あなたのために動作します。

+0

はい、そのように見えますが、私の場合は動作しません。 \tプロパティhibernate.archive.autodetectionの定義は次のとおりです: ".parアーカイブを解析中にHibernate Entity Managerによってどの要素が自動検出されるかを判別する(デフォルトはclass、hbm)"。しかし、.parアーカイブとは何ですか?私はそのようなarhiveを聞いたことがない。 – 1tox

+0

NB:元の投稿を更新しました – 1tox

+0

.parアーカイブはエンティティとpersistence.xmlをバンドルできる永続アーカイブファイルです。それは必須ではないので、心配しないでください(Hibernateのチームメンバーからの最後の投稿を参照してください:https://forum.hibernate.org/viewtopic.php?f=9&t=947671)。別の提案は、 falseを永続性ユニットに追加することです。 – Cody