Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need <class> elements in persistence.xml?

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?

like image 834
Michał Mech Avatar asked Nov 22 '09 22:11

Michał Mech


People also ask

Which elements belong to persistence unit in persistence xml?

xml file. 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.

What is considered optional while you define a persistence unit?

A JPA Persistence Unit is a logical grouping of user defined persistable classes (entity classes, embeddable classes and mapped superclasses) with related settings. Defining a persistence unit is optional when using ObjectDB, but required by JPA.

Where should I put persistence xml?

xml should be put in the EJB JAR's META-INF directory. If you package the persistence unit as a set of classes in a WAR file, persistence. xml should be located in the WAR file's WEB-INF/classes/META-INF directory.

Why do we need persistence xml?

The persistence. xml file must define a persistence-unit with a unique name in the current scoped classloader. The provider attribute specifies the underlying implementation of the JPA EntityManager. In JBoss AS, the default and only supported / recommended JPA provider is Hibernate.


1 Answers

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> 
like image 64
Pascal Thivent Avatar answered Oct 13 '22 17:10

Pascal Thivent