Alex Rivera | Logout

How to auto detect entities in JPA 2.0

Asked 2013-04-18T16:16:45.643
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?

Edit
Report

2 Answers

11

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>
answered 2013-04-18T16:26:36.707
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>

answered 2013-04-19T06:56:00.347

Your Answer