<!-- 配置持久化单元文件 -->
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<!-- 可以配置多个持久单元-->
<list>
<!-- 持久单元名称 Test -->
<value>classpath:config/common/persistences.xml</value>
<!-- 持久单元名称 coldIce -->
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<!-- 单配置多个持久单元的时候可以配置多个数据库连接池 -->
<!-- <entry key="remoteDataSource" value-ref="dataSource" /> -->
<property name="dataSources">
<map>
<entry key="localDataSource" value-ref="dataSource" />
</map>
</property>
<!--配置默认的数据源 -->
<property name="defaultDataSource" ref="dataSource" />
</bean>
<!-- 创建jpa实体管理工厂 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 存在一个或者多个持久单元的时候可以先配置持久单元块 然后再进行引用 -->
<!-- 为什这里已经参照了上面的持久话资源配置,底下还要指定一个具体的持久话单元名称,而且只能指定一个,那也就是说没法真正的配置多个持久化单元????? -->
<property name="persistenceUnitManager" ref="pum" />
<!-- 这里只能指定一个持久化单元 ,如果不指定 它就去自动去找一个默认的持久化单元,名称 为 “default” 然后系统就报错-->
<!--
<property name="persistenceUnitName" value="Test"/>
<property name="persistenceUnitName" value="coldIce"/>
-->
<!-- 只有一个持久单元的的时候可以直接在这里配置引用
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"/>
<property name="dataSource" ref="dataSource"/> -->
<property name="jpaProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
<!-- 事物管理器,引用entityManagerFactory对象,并对它进行管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 使用注解方式来使用声明事物 -->
<!-- 代理目标对象必须设置为 true 要不然sprig 注入的业务代理bean 就没有办法进行对象上转-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
寻求高手解答????
请大家多多赐教谢谢!!
//PersistenceUnitPostProcessor实现类,用于修改PersistenceUnitManager读取到的各个对应persistence.xml产生的PersistenceUnitInfo
package com.iwt.common.persist.jpa;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
import com.iwt.common.util.CommonUtil;
/**
* 合并多个JAR中的JPA实体到一个持久化单元
* <p>
* This merges all JPA entities from multiple jars. To use it, all entities must be listed in their respective
* persistence.xml files using the <class> tag.
*
* @see http://forum.springsource.org/showthread.php?t=61763 a
* @see http://javathoughts.capesugarbird.com/2009/02/jpa-and-multiple-persistence -units.html b
*/
public class MergingPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {
Map<String, List<String>> puiClasses = new HashMap<String, List<String>>();
String persistenceUnitName; //最终返回的持久化单元名字
List<String> includePersistenceUnitNames; //需要过滤的持久化单元名称列表
public MergingPersistenceUnitPostProcessor() {}
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
String pun = pui.getPersistenceUnitName();
System.out.println(pun);
if(!includePersistenceUnitNames.contains(pun)) {
pui.getManagedClassNames().clear();
}
List<String> classes = puiClasses.get(pun);
if (classes == null) {
classes = new ArrayList<String>();
puiClasses.put(pun, classes);
}
for (List<String> subClasses : puiClasses.values()) {
pui.getManagedClassNames().addAll(subClasses);
}
final List<String> names = pui.getManagedClassNames();
classes.addAll(names);
pui.setPersistenceUnitName(persistenceUnitName);
System.out.println("---------------------------------------------------------------");
System.out.println(pui);
for (String cn : names) {
System.out.println(cn);
}
System.out.println("---------------------------------------------------------------");
}
public void scanEntity() {}
//--------------------------------------------set-----------------------------------------------
public void setIncludePersistenceUnitNames(List<String> includePersistenceUnitNames) {
CommonUtil.checkArgument(includePersistenceUnitNames == null || includePersistenceUnitNames.isEmpty(), "需要合并的持久化单元名称列表不能为空!");
this.includePersistenceUnitNames = includePersistenceUnitNames;
}
public void setPersistenceUnitName(String persistenceUnitName) {
CommonUtil.checkArgument(persistenceUnitName == null || persistenceUnitName.isEmpty(), "合并后的持久化单元名称不能为空!");
this.persistenceUnitName = persistenceUnitName;
}
}
配置方法:
Java代码 收藏代码
<bean id=" persistenceUnitPostProcessor "
class="com.iwt.common.persist.jpa.MergingPersistenceUnitPostProcessor">
<!--需要合并的持久化单元名称列表,不能为空 -->
<property name="includePersistenceUnitNames">
<list value-type="java.lang.String" >
<value>huskyPU</value>
<value>huicardPU</value>
<value>huimerchantPU</value>
<value>taskCenterPU</value>
</list>
</property>
<!-- 最终返回的持久化单元名字,不能为空且需要与 entityManagerFactory的persistenceUnitName相同-->
<property name="persistenceUnitName" value="hui10PU" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="hui10PU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
<!-- Callback interface for post-processing a JPA PersistenceUnitInterface -->
<property name="persistenceUnitPostProcessors" ref="persistenceUnitPostProcessor"/>
</bean>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。