Spring Data JPA with Hibernate mapping files

hibernate, hibernate-mapping, java, spring, spring-data-jpa

Solution

Briefly

Your problem probably comes from the `mappingFileNamePattern` you provide. Try `**/*.hbm.xml` instead of `**hbm.xml`.

Complete snippet:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <!--<property name="persistenceUnitName" value="BLUPP" />-->
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
    <!-- <property name="packagesToScan" value="org.cleanyourway.server.beans" />-->
    <property name="persistenceUnitPostProcessors">
        <list>
            <bean
class="org.springframework.data.jpa.support.ClasspathScanningPersistenceUnitPostProcessor">
                <constructor-arg name="basePackage" value="org.xxxxxx.server.beans" />
                <property name="mappingFileNamePattern" value="**/*hbm.xml" />
            </bean>
        </list>
    </property>
</bean>

In details

Ant path patterns

Spring uses Ant path style patterns. You can find a good documentation on those patterns on the Ant Website. Double asterisk wildcard means: recurse in subdirectories. It should be followed by a slash as it stands for a directory.

ClasspathScanningPersistenceUnitPostProcessor

The mapping file detection part of `ClasspathScanningPersistenceUnitPostProcessor` takes the two parameters (`basePackage` (your constructors args) and `mappingFileNamePattern`) into account. With the suggested correction, Spring will search all **.hbm.xml* in subfolders org/xxxxxx/server/beans/ of the classpath.

Rephrased, you cannot expect that your `mappingFileNamePattern` would be interpreted alone for the search.

Hereunder, the code snippet of `ClasspathScanningPersistenceUnitPostProcessor` that makes the job:

String path = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
    + basePackage.replace('.', File.separatorChar)
    + File.separator + mappingFileNamePattern;

Small limitation of `ClasspathScanningPersistenceUnitPostProcessor`

You cannot scan for HBM files located at the root of JAR files in your classpath. `basePackage` doesn't support being empty and doesn't work with just a `"."` value.

Moreover, the underlying `PathMatchingResourcePatternResolver` doesn't work with Ant style path pattern with wilcard (`*` in you case) without a root directory (here and here (first warning in Other notes)).

Bug of `ClasspathScanningPersistenceUnitPostProcessor`

This class has never worked with Hibernate.

In the pre-1.4.x releases, there was this bug.

With this pull request, it seems there is a new bug that prevents me from getting the whole thing working with HBM in JARs. I got a `NullPointerException` at the line 146 because `resource.getURI().getPath();` doesn't seem to work with an URI with two : in the protocol (jar:file:/ in this case) and returns a `null` path.

(I will update my answer with a link to a bug report either when I have find one or posted one.)

Problem

I want to use Spring Data JPA with Hibernate mapping files and without JPA-Annotations. But I'am facing this exception on server startup (tomcat): ``` java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml} at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.obtainDefaultPersistenceUnitInfo(DefaultPersistenceUnitManager.java:547) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.determinePersistenceUnitInfo(LocalContainerEntityManagerFactoryBean.java:311) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:260) My dispatch-servlet.xml looks like the following: <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <!--<property name="persistenceUnitName" value="BLUPP" />--> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /> <!-- <property name="packagesToScan" value="org.cleanyourway.server.beans" />--> <property name="persistenceUnitPostProcessors"> <list> <bean class="org.springframework.data.jpa.support.ClasspathScanningPersistenceUnitPostProcessor"> <constructor-arg value="org.xxxxxx.server.beans" /> <property name="mappingFileNamePattern" value="**hbm.xml" /> </bean> </list> </property> </bean> ``` Is it possible to use Hibernate mapping files with the ClasspathScanningPersistenceUnitPostProcessor? I get it running with ``` <property name="packagesToScan" value="org.xxxxxxx.server.beans" /> ``` and JPA Annotations. Thanks for your help!

Original source

Related problems