問題描述
我有一個非常簡單的persistance.xml 文件:
<?xml version="1.0" encoding="UTF-8"?><持久化版本="1.0"xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"><persistence-unit name="eventtractor" transaction-type="RESOURCE_LOCAL"><class>pl.michalmech.eventtractor.domain.User</class><class>pl.michalmech.eventtractor.domain.Address</class><class>pl.michalmech.eventtractor.domain.City</class><class>pl.michalmech.eventtractor.domain.Country</class><屬性>
它有效.
但是當我刪除 <class>
元素時,應用程序看不到實體(所有類都用 @Entity
注釋).
是否有任何自動機制來掃描 @Entity
類?
persistence.xml 有一個你可以使用的 jar-file
.來自Java EE 5 教程:
<persistence><persistence-unit name=訂單管理"><描述>這個單元管理訂單和客戶.它不依賴于任何供應商特定的功能,并且可以因此可以部署到任何持久性提供程序.</描述><jta-data-source>jdbc/MyOrderDB</jta-data-source><jar-file>MyOrderApp.jar</jar-file><class>com.widgets.Order</class><class>com.widgets.Customer</class></持久化單元></持久性>
這個文件定義了一個持久化單元命名為 OrderManagement
,它使用JTA 感知數據源 jdbc/MyOrderDB
.jar-file
和 class
元素指定托管持久性類:實體類、可嵌入類和映射超類.jar-file
元素指定對包含托管持久性類的打包持久性單元可見的 JAR 文件,而 class
元素顯式命名托管持久性類.
如果是 Hibernate,請查看 第二章.設置和配置了解更多詳情.
實際上,如果您不介意不符合規范,Hibernate 甚至在 Java SE 中也支持自動檢測.為此,請添加 hibernate.archive.autodetection
屬性:
<persistence-unit name="eventtractor"事務類型=RESOURCE_LOCAL"><!-- 這需要符合規范,但 Hibernate 支持即使在 JSE 中也可以自動檢測.<class>pl.michalmech.eventtractor.domain.User</class><class>pl.michalmech.eventtractor.domain.Address</class><class>pl.michalmech.eventtractor.domain.City</class><class>pl.michalmech.eventtractor.domain.Country</class>--><屬性><!-- 掃描帶注釋的類和 Hibernate 映射 XML 文件 --><屬性名稱=hibernate.archive.autodetection"值=類,hbm"/><屬性名稱=hibernate.hbm2ddl.auto"值=驗證";/><屬性名稱=hibernate.show_sql";值=真";/></屬性></持久化單元>
I have very simple persistance.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
and it works.
But when I remove <class>
elements application doesn't see entities (all classes are annotated with @Entity
).
Is there any automatic mechanism to scan for @Entity
classes?
The persistence.xml has a jar-file
that you can use. From the Java EE 5 tutorial:
<persistence> <persistence-unit name="OrderManagement"> <description>This unit manages orders and customers. It does not rely on any vendor-specific features and can therefore be deployed to any persistence provider. </description> <jta-data-source>jdbc/MyOrderDB</jta-data-source> <jar-file>MyOrderApp.jar</jar-file> <class>com.widgets.Order</class> <class>com.widgets.Customer</class> </persistence-unit> </persistence>
This file defines a persistence unit
named OrderManagement
, which uses a
JTA-aware data source jdbc/MyOrderDB
. The jar-file
and class
elements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses. The jar-file
element specifies JAR files that are visible to the packaged persistence unit that contain managed persistence classes, while the class
element explicitly names managed persistence classes.
In the case of Hibernate, have a look at the Chapter2. Setup and configuration too for more details.
EDIT: Actually, If you don't mind not being spec compliant, Hibernate supports auto-detection even in Java SE. To do so, add the hibernate.archive.autodetection
property:
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<!-- This is required to be spec compliant, Hibernate however supports
auto-detection even in JSE.
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
-->
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
這篇關于我需要<class>persistence.xml 中的元素?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!