我需要<类别和GT; persistence.xml中的元素? [英] Do I need <class> elements in persistence.xml?

查看:336
本文介绍了我需要<类别和GT; persistence.xml中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很简单的persistance.xml文件:

<?XML版本=1.0编码=UTF-8&GT?;
<持久性版本=1.0
    的xmlns =htt​​p://java.sun.com/xml/ns/persistence的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance
    XSI:的schemaLocation =htt​​p://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd\">    <持久性单位名称=eventractor交易型=RESOURCE_LOCAL>
        <类别和GT; pl.michalmech.eventractor.domain.User< /班>
        <类别和GT; pl.michalmech.eventractor.domain.Address< /班>
        <类别和GT; pl.michalmech.eventractor.domain.City< /班>
        <类别和GT; pl.michalmech.eventractor.domain.Country< /班>        <性状>
            <属性名=hibernate.hbm2ddl.autoVALUE =验证/>
            <属性名=hibernate.show_sqlVALUE =真/>
        < /性状>
    < /持久化部>< /持久>

和它的作品。

但是,当我删除<类> 元素的应用程序没有看到实体(所有的类都标注了 @Entity )。

有没有自动机制来扫描 @Entity 类?


解决方案

persistence.xml中有一个 JAR文件,您可以使用。从了Java EE 5教程


  

<坚持不懈,GT;
    <持久性单位名称=OrderManagement>
        <描述>这单元管理订单和客户。
            它不依赖于任何特定供应商的特点和CAN
            因此可以部署到任何持久性提供。
        < /描述>
        < JTA数据源>的jdbc / MyOrderDB&​​LT; / JTA数据源>
        < JAR-文件>&MyOrderApp.jar LT; / JAR-文件>
        <类别和GT; com.widgets.Order< /班>
        <类别和GT; com.widgets.Customer< /班>
    < /持久化部>
< /持久>


此文件定义了一个持久单元
名为 OrderManagement ,它采用了
JTA感知的数据源 JDBC / MyOrderDB 。在 JAR文件元素指定管理的持久化类:实体类,嵌入类,并映射超类。在 JAR文件元素指定JAR文件都包含托管持久类封装持久单元可见,而元素明确指定管理的持久化类。

在休眠的情况下,有一个看的Chapter2.设置和配置太了解更多详情。

编辑:其实,如果你不介意的话不是遵循规范,Hibernate支持自动检测,甚至在Java SE。要做到这一点,添加 hibernate.archive.autodetection 属性:

<持久性单位名称=eventractor交易型=RESOURCE_LOCAL>
  <! - 这是要求是遵循规范,然而,Hibernate支持
       自动检测,即使在JSE。
  <类别和GT; pl.michalmech.eventractor.domain.User< /班>
  <类别和GT; pl.michalmech.eventractor.domain.Address< /班>
  <类别和GT; pl.michalmech.eventractor.domain.City< /班>
  <类别和GT; pl.michalmech.eventractor.domain.Country< /班>
    - >  <性状>
    <! - 扫描注释类和Hibernate映射XML文件 - >
    <属性名=hibernate.archive.autodetectionVALUE =级,HBM/>    <属性名=hibernate.hbm2ddl.autoVALUE =验证/>
    <属性名=hibernate.show_sqlVALUE =真/>
  < /性状>
< /持久化部>

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>

这篇关于我需要&LT;类别和GT; persistence.xml中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆