11

I am pretty sure that I used some sort of auto detection of beans annotated with @Entity in JPA 2.0 in the past but I am not able to find out how. How do you do that instead of listing each bean in a class XML element in the persistence.xml?

LuckyLuke
  • 47,771
  • 85
  • 270
  • 434

3 Answers3

23

You need add to the persistence.xml the next line:

<exclude-unlisted-classes>false</exclude-unlisted-classes>

e.g.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" ...>
    <persistence-unit name="YourPU" ...>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.logging.level" value="ALL"/>
            <property name="eclipselink.ddl-generation" 
                value="drop-and-create-tables"/>
        </properties>
    </persistence-unit>
</persistence>
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
10

Since Spring 3.1, you also have the option to forget persistence.xml altogether, and configure your EntityManagerFactory using the packagesToScan property, similar to this:

<bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource"
      p:packagesToScan="${jpa.entity.packages}">

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
              p:showSql="${hibernate.show_sql}"/>
    </property>

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
    </property>
</bean>
Arend v. Reinersdorff
  • 4,110
  • 2
  • 36
  • 40
zagyi
  • 17,223
  • 4
  • 51
  • 48
0

See Pascal Thivent answer here : Do I need <class> elements in persistence.xml?

you have different way to do it, but JPA itself does not support auto-scan. The simplest and cleanest way to reference your entities IMHO is to package your model in a jar and to reference it with <jar-file>MyModel.jar</jar-file>

Community
  • 1
  • 1
Gab
  • 7,869
  • 4
  • 37
  • 68