我有一个非常简单的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模板网!