Home > Java > javaTutorial > How can I define entities in persistence.xml for JPA?

How can I define entities in persistence.xml for JPA?

DDD
Release: 2024-11-28 08:02:10
Original
461 people have browsed it

How can I define entities in persistence.xml for JPA?

Entities in persistence.xml

In Java Persistence API (JPA), the persistence.xml file tells the persistence provider about the persistence units in the application and serves as a configuration file. One of its functions is to define the entities to be managed by the persistence unit.

Automatic Entity Scanning

JPQL automatically scans for entities in the Java packages with the classes specified in the elements of persistence.xml. However, this approach is not mandatory, and other mechanisms can be used to define the entities.

Using the jar-file Element

Instead of listing the entities manually, you can use the jar-file element to specify one or more JAR files containing the entity classes. The persistence provider will scan these JAR files for classes annotated with @Entity.

<persistence>
    <persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
        <jar-file>MyOrderApp.jar</jar-file>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>
Copy after login

Hibernate Auto-Detection

Hibernate specifically supports auto-detection of entities even in Java SE applications. To enable this, add the hibernate.archive.autodetection property to persistence.xml:

<persistence>
    <persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
        <!-- Hibernate auto-detection is spec compliant in Java EE only. -->
        <properties>
            <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>
</persistence>
Copy after login

Conclusion

While it is not mandatory to specify entities directly in persistence.xml, doing so ensures explicit control over which classes are managed by the persistence provider. Using jar-file elements or Hibernate's auto-detection feature provides more flexibility in managing entities.

The above is the detailed content of How can I define entities in persistence.xml for JPA?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template