原文地址:http://www.cnblogs.com/taven/p/3351841.html
JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="pilicat_app_jpa" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.jdbc.fetch_size" value="50" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
private
List<SpringPersistenceUnitInfo> readPersistenceUnitInfos() {
List<SpringPersistenceUnitInfo> infos =
new
LinkedList<SpringPersistenceUnitInfo>();
boolean
buildDefaultUnit = (
this
.packagesToScan !=
null
||
this
.mappingResources !=
null
);
PersistenceUnitReader reader =
new
PersistenceUnitReader(
this
.resourcePatternResolver,
this
.dataSourceLookup);
SpringPersistenceUnitInfo[] readInfos = reader.readPersistenceUnitInfos(
this
.persistenceXmlLocations);
for
(SpringPersistenceUnitInfo readInfo : readInfos) {
infos.add(readInfo);
if
(
this
.defaultPersistenceUnitName !=
null
&&
this
.defaultPersistenceUnitName.equals(readInfo.getPersistenceUnitName())) {
buildDefaultUnit =
false
;
}
}
if
(buildDefaultUnit) {
infos.add(buildDefaultPersistenceUnitInfo());
}
return
infos;
}
|
注意看这个源码的方法,defaultPersistenceUnitName 变量如果不为空,并且等于 persistence 配置文件中的持久化单元名称,则buildDefaultUnit就为false,buildDefaultUnit 如果为 false,是不会执行 buildDefaultPersistenceUnitInfo() 方法的,而 buildDefaultPersistenceUnitInfo() 方法是根据我们定义的 packagesToScan 去自动扫描Entity实体类的。