Home >Java >javaTutorial >Is `` Required in `persistence.xml` for Entity Management?

Is `` Required in `persistence.xml` for Entity Management?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-30 16:00:20863browse

Is `` Required in `persistence.xml` for Entity Management?

Is Required in persistence.xml?

In a standard persistence.xml file, elements are necessary to specify managed persistence classes. Without them, the application cannot recognize entities, even with @Entity annotations present.

Automatic Class Scanning

However, the Java EE specification 5 includes a jar-file element within persistence.xml that can be used for automatic scanning of persistence classes:

<persistence>
  <persistence-unit name="UnitName">
    <jar-file>MyJarFile.jar</jar-file>
    <class>com.example.EntityClass</class>
  </persistence-unit>
</persistence>

Hibernate Auto-Detection

If a spec-compliant approach is not preferred, Hibernate supports auto-detection in Java SE environments:

<persistence-unit name="UnitName">
  <properties>
    <property name="hibernate.archive.autodetection" value="class, hbm" />
    ...
  </properties>
</persistence-unit>

This property scans for annotated classes and Hibernate mapping XML files to automatically discover entities.

Note: For Hibernate versions prior to 5.1, this requires adding the Hibernate EntityManager Bean 2 module as a dependency.

The above is the detailed content of Is `` Required in `persistence.xml` for Entity Management?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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