Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto detect entities in JPA 2.0

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?

like image 323
LuckyLuke Avatar asked Apr 18 '13 16:04

LuckyLuke


2 Answers

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>
like image 53
zagyi Avatar answered Nov 02 '22 16:11

zagyi


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>
like image 23
Paul Vargas Avatar answered Nov 02 '22 16:11

Paul Vargas