spring mvc jpa 引入包中的实体类

http://www.iteye.com/topic/1131643

最近在做一个web项目. 大系统下有三个子Web模块. 
打包时会生成webA.war, webB.war, webC.war. 
因为三个子模块有共用的model, 所以我将共有的model层提取到一个公共的模块中:model 
系统用maven构建.关系为 

parent 
|------pom.xml 
|------model 
          |------pom.xml 
|------webA 
          |------pom.xml 
          |------...META-INF/persistence.xml 
|------webB 
          |------pom.xml 
          |------...META-INF/persistence.xml 
|------webC 
          |------pom.xml 
          |------...META-INF/persistence.xml 

在抽取共用model模块之前,web中的JPA(Hibernate实现)使用了注解进行entity的声明. 
所以persistence.xml的配置很简单:

Xml代码   收藏代码
  1. <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">  
  2.         <provider>org.hibernate.ejb.HibernatePersistence</provider>          
  3.         <properties>  
  4.             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>  
  5.             <property name="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"/>  
  6.             <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>  
  7.             <property name="hibernate.connection.charSet" value="UTF-8"/>  
  8.          </properties>  
  9.     </persistence-unit>  
  10. </persistence>  

 但在引入公用的包,再运行服务器,会报出"Caused by: org.hibernate.MappingException: Unknown entity:  ***"的错误.

 经过Google发现,原因是使用Hibernate 如果存在实体在web引用的jar包中,必须在persistence.xml声明这些实体:

Xml代码   收藏代码
  1. <persistence ...>  
  2.     <persistence-unit ...>   
  3.         <class>x.y.z.model.A</class>               
  4.         <class>x.y.z.model.B</class>  
  5.         <class>x.y.z.model.C</class>  
  6.         ....  
  7.     </persistence-unit>  
  8. </persistence>  

  

 但我们的体统中的实体数量并不在少数.有接近100个之多.随着新业务的开发,实体数量还会继续添加.一个一个的声明太过于麻烦.但遗憾的是hibernate在这里并不支持正则表达式.

幸好jpa(hibernate)支持,引入jar. 并在jar中自动查找所有实体,只需要这样声明:

Xml代码   收藏代码
  1. <persistence ...>  
  2.         <jar-file>classpath:../lib/model.jar</jar-file>  
  3. </persistence>  

经过这样修改,服务又能正常启动了.

但这样的修改引入了另一个问题. 之前的Junit 集成测试不能使用了.

Junit使用了spring对junit4的注解.

Java代码   收藏代码
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext.xml")  
  3. public class XXXIntegrationTest {  
  4.   
  5.     @Test  
  6.     public void testA() {......}  
  7.   
  8. }  

 不论是在maven下,还是在eclipse中,都会提示java.net.MalformedURLException: unknown protocol: classpath ,而刚才的对persistence.xml的修改正好此入了classpath:..相关的代码.而spring-test的相关类对这样的设置并没有正确的处理. 只有将<jar-file>classpath:../lib/model.jar</jar-file>中的classpath改为开头的file:的路径,spring-test才能正确识别. 
所以在测试时,只能将 persistence.xml再改为: <jar-file>file:D:\.m2\repository\...\model.jar<jar-file>才能正确执行.

在打包和测试时,来回修改persistence.xml文件是一件很麻烦的事.只能通过maven的profile来解决了.所以在web模块的pom中加入以下声明.

Xml代码   收藏代码
  1. <project ...>  
  2.     <properties>  
  3. ....  
  4.         <jpa.jar.file>file:${settings.localRepository}/package.../${project.version}/model-${project.version}.jar</jpa.jar.file>  
  5.     </properties>  
  6.   
  7. <build>  
  8.     <plugins>  
  9.           <plugin>  
  10.                 <artifactId>maven-antrun-plugin</artifactId>  
  11.                 <version>1.7</version>  
  12.                 <executions>  
  13.                     <execution>  
  14.                         <phase>process-resources</phase>  
  15.                         <configuration>  
  16.                             <tasks>  
  17.                                 <replace token="@ENTITY-JAR-FILE@" value="${jpa.jar.file}" dir="target/classes/META-INF">  
  18.                                     <include name="**/persistence.xml" />  
  19.                                 </replace>  
  20.                             </tasks>  
  21.                         </configuration>  
  22.                         <goals>  
  23.                             <goal>run</goal>  
  24.                         </goals>  
  25.                     </execution>  
  26.                 </executions>  
  27.               </plugin>  
  28.         </plugins>  
  29.   </build>  
  30.   
  31.   
  32.   <profile>  
  33.     <id>prod</id>  
  34.     <properties>  
  35.         <jpa.jar.file>classpath:../lib/model-${project.version}.jar</jpa.jar.file>  
  36.     </properties>  
  37.   
  38.   </profile>  
  39. </project>  

 并在persistence.xml中的jar-file以下修改:

Xml代码   收藏代码
  1. <jar-file>@ENTITY-JAR-FILE@</jar-file>  

 
这样打包和开发就都能顺利进行了.

但回头想想. 解决这个问题还是费了不少时间.

如果一开始直接在persistence.xml定义一串很长的实体列表.虽然看起来比较笨,也不会引入新的问题.其实也挺省时间.

声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。

http://www.iteye.com/topic/1131643

最近在做一个web项目. 大系统下有三个子Web模块. 
打包时会生成webA.war, webB.war, webC.war. 
因为三个子模块有共用的model, 所以我将共有的model层提取到一个公共的模块中:model 
系统用maven构建.关系为 

parent 
|------pom.xml 
|------model 
          |------pom.xml 
|------webA 
          |------pom.xml 
          |------...META-INF/persistence.xml 
|------webB 
          |------pom.xml 
          |------...META-INF/persistence.xml 
|------webC 
          |------pom.xml 
          |------...META-INF/persistence.xml 

在抽取共用model模块之前,web中的JPA(Hibernate实现)使用了注解进行entity的声明. 
所以persistence.xml的配置很简单:

Xml代码   收藏代码
  1. <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">  
  2.         <provider>org.hibernate.ejb.HibernatePersistence</provider>          
  3.         <properties>  
  4.             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>  
  5.             <property name="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"/>  
  6.             <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>  
  7.             <property name="hibernate.connection.charSet" value="UTF-8"/>  
  8.          </properties>  
  9.     </persistence-unit>  
  10. </persistence>  

 但在引入公用的包,再运行服务器,会报出"Caused by: org.hibernate.MappingException: Unknown entity:  ***"的错误.

 经过Google发现,原因是使用Hibernate 如果存在实体在web引用的jar包中,必须在persistence.xml声明这些实体:

Xml代码   收藏代码
  1. <persistence ...>  
  2.     <persistence-unit ...>   
  3.         <class>x.y.z.model.A</class>               
  4.         <class>x.y.z.model.B</class>  
  5.         <class>x.y.z.model.C</class>  
  6.         ....  
  7.     </persistence-unit>  
  8. </persistence>  

  

 但我们的体统中的实体数量并不在少数.有接近100个之多.随着新业务的开发,实体数量还会继续添加.一个一个的声明太过于麻烦.但遗憾的是hibernate在这里并不支持正则表达式.

幸好jpa(hibernate)支持,引入jar. 并在jar中自动查找所有实体,只需要这样声明:

Xml代码   收藏代码
  1. <persistence ...>  
  2.         <jar-file>classpath:../lib/model.jar</jar-file>  
  3. </persistence>  

经过这样修改,服务又能正常启动了.

但这样的修改引入了另一个问题. 之前的Junit 集成测试不能使用了.

Junit使用了spring对junit4的注解.

Java代码   收藏代码
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext.xml")  
  3. public class XXXIntegrationTest {  
  4.   
  5.     @Test  
  6.     public void testA() {......}  
  7.   
  8. }  

 不论是在maven下,还是在eclipse中,都会提示java.net.MalformedURLException: unknown protocol: classpath ,而刚才的对persistence.xml的修改正好此入了classpath:..相关的代码.而spring-test的相关类对这样的设置并没有正确的处理. 只有将<jar-file>classpath:../lib/model.jar</jar-file>中的classpath改为开头的file:的路径,spring-test才能正确识别. 
所以在测试时,只能将 persistence.xml再改为: <jar-file>file:D:\.m2\repository\...\model.jar<jar-file>才能正确执行.

在打包和测试时,来回修改persistence.xml文件是一件很麻烦的事.只能通过maven的profile来解决了.所以在web模块的pom中加入以下声明.

Xml代码   收藏代码
  1. <project ...>  
  2.     <properties>  
  3. ....  
  4.         <jpa.jar.file>file:${settings.localRepository}/package.../${project.version}/model-${project.version}.jar</jpa.jar.file>  
  5.     </properties>  
  6.   
  7. <build>  
  8.     <plugins>  
  9.           <plugin>  
  10.                 <artifactId>maven-antrun-plugin</artifactId>  
  11.                 <version>1.7</version>  
  12.                 <executions>  
  13.                     <execution>  
  14.                         <phase>process-resources</phase>  
  15.                         <configuration>  
  16.                             <tasks>  
  17.                                 <replace token="@ENTITY-JAR-FILE@" value="${jpa.jar.file}" dir="target/classes/META-INF">  
  18.                                     <include name="**/persistence.xml" />  
  19.                                 </replace>  
  20.                             </tasks>  
  21.                         </configuration>  
  22.                         <goals>  
  23.                             <goal>run</goal>  
  24.                         </goals>  
  25.                     </execution>  
  26.                 </executions>  
  27.               </plugin>  
  28.         </plugins>  
  29.   </build>  
  30.   
  31.   
  32.   <profile>  
  33.     <id>prod</id>  
  34.     <properties>  
  35.         <jpa.jar.file>classpath:../lib/model-${project.version}.jar</jpa.jar.file>  
  36.     </properties>  
  37.   
  38.   </profile>  
  39. </project>  

 并在persistence.xml中的jar-file以下修改:

Xml代码   收藏代码
  1. <jar-file>@ENTITY-JAR-FILE@</jar-file>  

 
这样打包和开发就都能顺利进行了.

但回头想想. 解决这个问题还是费了不少时间.

如果一开始直接在persistence.xml定义一串很长的实体列表.虽然看起来比较笨,也不会引入新的问题.其实也挺省时间.

猜你喜欢

转载自endual.iteye.com/blog/1942914