Merging persistence units
Spring 支持使用多个持久化单元。但是,有时您可能希望对您的应用程序进行模块化,但仍确保所有这些模块都运行在单个持久化单元内。为了实现该行为,Spring Data JPA 提供了一个 PersistenceUnitManager
实现,该实现基于其名称自动合并持久化单元,如下例所示:
Spring supports having multiple persistence units. Sometimes, however, you might want to modularize your application but still make sure that all these modules run inside a single persistence unit. To enable that behavior, Spring Data JPA offers a PersistenceUnitManager
implementation that automatically merges persistence units based on their name, as shown in the following example:
.Using MergingPersistenceUnitmanager
<bean class="….LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager">
<bean class="….MergingPersistenceUnitManager" />
</property>
</bean>
Classpath Scanning for @Entity Classes and JPA Mapping Files
纯 JPA 设置要求所有注释映射的实体类都列在 orm.xml
中。同样也适用于 XML 映射文件。Spring Data JPA 提供了一个 ClasspathScanningPersistenceUnitPostProcessor
,它获取配置的基本包,并可以选取一个映射文件名模式。然后,它会扫描指定包中注释了 @Entity
或 @MappedSuperclass
的类,加载与文件名模式匹配的配置文件,并将它们交给 JPA 配置。必须如下配置后置处理器:
A plain JPA setup requires all annotation-mapped entity classes to be listed in orm.xml
. The same applies to XML mapping files. Spring Data JPA provides a ClasspathScanningPersistenceUnitPostProcessor
that gets a base package configured and optionally takes a mapping filename pattern. It then scans the given package for classes annotated with @Entity
or @MappedSuperclass
, loads the configuration files that match the filename pattern, and hands them to the JPA configuration. The post-processor must be configured as follows:
<bean class="….LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitPostProcessors">
<list>
<bean class="org.springframework.data.jpa.support.ClasspathScanningPersistenceUnitPostProcessor">
<constructor-arg value="com.acme.domain" />
<property name="mappingFileNamePattern" value="**/*Mapping.xml" />
</bean>
</list>
</property>
</bean>
从 Spring 3.1 开始,可以在 |
As of Spring 3.1, a package to scan can be configured on the |