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:

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.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan">
  <!-- <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" />

And even wildcards are possible!

Leave a Reply

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