The recently released Spring 3.1 opens up the possibility to scan the whole classpath for JPA entities. The idea is to bootstrap JPA programmatically without a persistence.xml file. You can find details about the new feature here: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/new-in-3.1.html#new-in-3.1-jpa-without-xml

Instead of referring to a persistence unit you can simply define the Java packages containing your entity files, when creating a LocalContainerEntityManagerFactoryBean. And the whole classpath is scanned.

 highlight="3,6"]
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan">
    <array>
      <value>at.nonblocking.test.model<value>
      <value>at.nonblocking.test2.**.model<value>
    <array>
  <!-- <property name="persistenceUnitName" value="myPersistenceUnit" /> -->
  <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
      <property name="generateDdl" value="true" />
      <property name="showSql" value="true" />
    </bean>
  </property>
</bean>

And even wildcards are possible!

Leave a Reply

Your email address will not be published. Required fields are marked *


*