Mic*_*ech 106 java orm annotations hibernate jpa
我有非常简单的persistance.xml文件:
<?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>
Run Code Online (Sandbox Code Playgroud)
它的工作原理.
但是当我删除<class>元素时,应用程序看不到实体(所有类都带有注释@Entity).
是否有任何自动机制来扫描@Entity类?
Pas*_*ent 78
persistence.xml有一个jar-file你可以使用的.从Java EE 5教程:
Run Code Online (Sandbox Code Playgroud)<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>
此文件定义名为的持久性单元OrderManagement,该单元使用支持JTA的数据源jdbc/MyOrderDB.所述jar-file和class元素指定管持久类:实体类,可嵌入类和超类映射.该jar-file元素指定对包含托管持久性类的打包持久性单元可见的JAR文件,而该class元素显式指定托管持久性类.
在Hibernate的情况下,请看第2章.设置和配置也有更多详细信息.
编辑:实际上,如果你不介意不符合规范,Hibernate甚至在Java SE中也支持自动检测.为此,请添加hibernate.archive.autodetection属性:
<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>
Run Code Online (Sandbox Code Playgroud)
Mad*_*bæk 43
在Java SE环境中,按照规范,您必须像以前一样指定所有类:
必须在Java SE环境中指定所有命名的托管持久性类的列表,以确保可移植性
和
如果不打算将持久性单元的根中包含的带注释的持久性类包含在持久性单元中,则应使用exclude-unlisted-classes元素.exclude-unlisted-classes元素不适用于Java SE环境.
(JSR-000220 6.2.1.6)
在Java EE环境中,您不必执行此操作,因为提供程序会为您扫描注释.
非正式地,您可以尝试<exclude-unlisted-classes>false</exclude-unlisted-classes>在persistence.xml中进行设置.此参数默认为falseEE和trueSE.双方的EclipseLink和Toplink的支持这是到目前为止,我可以告诉.但如上所述,根据规范,你不应该依赖它在SE工作.
您可以尝试以下操作(在SE环境中可能有效,也可能无效):
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)
abb*_*bas 13
我是否需要persistence.xml中的Class元素?
不,你不一定.以下是您在Eclipse中的使用方法(Kepler测试):
右键单击项目,单击Properties,选择JPA,在Persistence类管理中自动选中Discover annotated classes.
小智 8
您可以提供jar-file具有已编译类的文件夹的元素路径.例如,当我为一些集成测试准备persistence.xml时,我添加了类似的东西:
<jar-file>file:../target/classes</jar-file>
Run Code Online (Sandbox Code Playgroud)
对于JPA 2+,这就是诀窍
<jar-file></jar-file>
Run Code Online (Sandbox Code Playgroud)
扫描战争中所有罐子的注释@Entity类
Hibernate不支持<exclude-unlisted-classes>false</exclude-unlisted-classes>SE,(另一张海报提到这适用于TopLink和EclipseLink).
有些工具可以自动生成persistence.xml的类列表,例如IntelliJ中的Import Database Schema向导.在persistence.xml中获得项目的初始类之后,随着项目的进展,手动添加/删除单个类应该很简单.
| 归档时间: |
|
| 查看次数: |
146361 次 |
| 最近记录: |