Spring JPA data with Hibernate mapping files

I want to use Spring Data JPA with Hibernate mapping files and without JPA annotations.

But I encounter this exception when starting the server (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> 

Can I use Hibernate mapping files with the ClasspathScanningPersistenceUnitPostProcessor class?

I run it with

 <property name="packagesToScan" value="org.xxxxxxx.server.beans" /> 

and JPA Annotations.

Thank you for your help!

+4
source share
1 answer

Short

Your problem is probably related to the mappingFileNamePattern that you provide. Try **/*.hbm.xml instead of **hbm.xml .

Full 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> 

More details

Ant Path Templates

Spring uses Ant style patterns. You can find good documentation on these templates on the Ant Website . A double asterisk means: recurse in subdirectories. It is followed by a slash, as it denotes a directory.

ClasspathScanningPersistenceUnitPostProcessor

The ClasspathScanningPersistenceUnitPostProcessor mapping file detection part takes into account two parameters ( basePackage (your args constructors) and mappingFileNamePattern ). With the proposed correction, Spring will look for everything **. Hbm.xml * in subfolders of org / xxxxxx / server / beans / classpath.

To paraphrase, you cannot expect your mappingFileNamePattern be interpreted for search only.

Next, the code snippet ClasspathScanningPersistenceUnitPostProcessor that performs the job:

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

Small limitation of ClasspathScanningPersistenceUnitPostProcessor

You cannot scan HBM files located at the root of the JAR files in your class path. basePackage does not support empty and does not work only with the value "." .

In addition, the base PathMatchingResourcePatternResolver does not work with the Ant style template template with wilcard ( * in your case) without a root directory ( here and here (first warning in other notes)).

ClasspathScanningPersistenceUnitPostProcessor Error

This class has never worked with Hibernate.

In releases prior to 1.4.x there was this error .

With this stretch request , there seems to be a new bug that is stopping me from getting everything that works with HBM in the JAR. I got a NullPointerException on line 146 because resource.getURI().getPath(); doesn't seem to work with a 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 the bug report when I find it or post it.)

+4
source

All Articles