JPA实体作为可共享的jar不在WAR文件中运行,而不是Tomcat 7.0.27

时间:2013-03-28 15:06:37

标签: tomcat jpa jar war entities

我试图在eclipse Juno的JPA 2.0项目中共享我的实体,因为我正在创建一个测试项目,我需要访问然后在那里进行测试,如果我创建任何其他项目,可以重用这些实体

最大的问题是hibernate(4.1.9)可以在相对路径中找到我的jar,但最奇怪的是在我的测试项目中只使用Spring 3 hibernate完全读取我的实体jar和hbm文件。但是当我尝试使用tomcat 7.0.27对真实项目做同样的事情时,它可以在jar的地方定位,我的真实项目是tomcat和Spring 3中的常见WAR文件。这是我的实际项目的persistence.xml:

<?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="CustomerServiceMacrosUnit"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- the JNDI data source -->
    <non-jta-data-source>java:comp/env/jdbc/CustomerServiceMacrosDS</non-jta-data-source>

    <jar-file>../lib/aliancaModel.jar</jar-file>

    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

这是我对所有类映射的jar持久性:

<?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="AliancaJPA">
       <class>br.com.alianca.customerservicemacros.entity.controlefaturas.DocsysBl</class>
       <class>br.com.alianca.customerservicemacros.entity.controlefaturas.DocsysBooking</class>
      <class>br.com.alianca.customerservicemacros.entity.controlefaturas.DocsysCharge</class>
       <class>br.com.alianca.customerservicemacros.entity.controlefaturas.DocsysContainer</class>

</persistence-unit>

以下是tomcat日志中的错误:

INFO: Initializing Spring root WebApplicationContext
28/03/13 12:00  INFO org.hibernate.annotations.common.Version:37
HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
28/03/13 12:00  INFO org.hibernate.Version:41
HHH000412: Hibernate Core {4.1.9.Final}
28/03/13 12:00  INFO org.hibernate.cfg.Environment:239
HHH000206: hibernate.properties not found
28/03/13 12:00  INFO org.hibernate.cfg.Environment:342
HHH000021: Bytecode provider name : javassist
28/03/13 12:01  WARN org.hibernate.ejb.packaging.FileZippedJarVisitor:74
HHH015010: Unable to find file (ignored): file:../lib/aliancaModel.jar
java.io.FileNotFoundException: ..\lib\aliancaModel.jar (The system cannot find the path specified)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:127)
at java.util.jar.JarFile.<init>(JarFile.java:135)
at java.util.jar.JarFile.<init>(JarFile.java:72)
at org.hibernate.ejb.packaging.FileZippedJarVisitor.doProcessElements(FileZippedJarVisitor.java:70)
at org.hibernate.ejb.packaging.AbstractJarVisitor.getMatchingEntries(AbstractJarVisitor.java:149)
at org.hibernate.ejb.packaging.NativeScanner.getClassesInJar(NativeScanner.java:128)
at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configuration.java:485)
at org.hibernate.ejb.Ejb3Configuration.addMetadataFromScan(Ejb3Configuration.java:475)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:371)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:92)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1525)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1463)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:99)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findNamedEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:512)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:494)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:659)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:630)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:340)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1117)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

使用调试,我可以强制hibernate在我的文件系统中使用完整路径来定位jar,但即使使用资源它也找不到我的hbms!

我开始认为它只能用于EAR文件! :( 有人可以给点意见吗?

1 个答案:

答案 0 :(得分:1)

最后我想出了如何解决这个问题!

我不知道为什么,但是如果你使用“LocalContainerEntityManagerFactoryBean”,它可以正确地找到并读取实体的jar,所以我不得不改变我的弹簧配置:

在:

<bean id="emfCustomerServiceMacros" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="CustomerServiceMacrosUnit" />
</bean>

现在有了这种方法,spring和hibernate可以找到jar:

<bean id="emfCustomerServiceMacros" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">  
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />  
</property>  
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />  
</bean>  

现在如果hibernate找不到jar,它会在日志中显示一个完整的路径,它会在哪里查看库,在我的例子中,我将我的web应用程序的persistence.xml更正为:

<?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="CustomerServiceMacrosUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- the JNDI data source -->
<non-jta-data-source>java:comp/env/jdbc/CustomerServiceMacrosDS</non-jta-data-source>

<jar-file>lib/AliancaModel.jar</jar-file>

我会查看Spring的源代码,看看为什么会这样!