Get JPA2 running on WebLogic 10.3

When you try to get JPA2 running on WebLogic 10.3 you might get this exception:

java.lang.NoSuchMethodError: javax/persistence/spi/PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:632)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:72)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)

The reason is that WebLogic loads all JPA classes with the system classloader but ships JPA1. You have to force WebLogic to load the classes from within the lib folder of your web application.

Add to WEB-INF/weblogic.xml in your WAR project:

<wls:container-descriptor>
       <wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
<wls:container-descriptor>

And additionally add to META-INF/weblogic-application.xml in your EAR project:

<wls:prefer-application-packages>
	<wls:package-name>javax.persistence.*</wls:package-name>
	<wls:package-name>org.hibernate.*</wls:package-name>
	<wls:package-name>org.springframework.*</wls:package-name>
</wls:prefer-application-packages>

It seems both is necessary.

Load JPA Entites from within multiple projects

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!