JPA entities as a shareable jar not working in a W

2019-07-21 04:38发布

问题:

i tried to share my entities in a JPA 2.0 project in eclipse Juno, ´cause i´m creating a test project and i need to access then there to make my tests and if i create any other project a can reuse those entities.

The biggest problem is that hibernate ( 4.1.9 ) can find my jar in a relative path, but the most strange is that in my test project using only Spring 3 hibernate read my entities jar and hbm files perfectly. But when i try to do the same with the real project using tomcat 7.0.27 it can locale where the jar is, my real project is a common WAR file in tomcat and with Spring 3. Here is my persistence.xml of real project:

<?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>

And here is my jar persistence with all classes mapped:

<?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>

Here is the error from tomcat´s log:

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)

Using the debug, i could force hibernate to locale the jar with full path in my filesystem, but even using the resource it couldn´t find my hbms!

I starting to think that it can only work wit EAR files! :( Can someone give some point?

回答1:

finally i figure out how to manage this problem!

I don´t known why, but if you use "LocalContainerEntityManagerFactoryBean", it can find and read the entities´s jar properly, so i had to change my spring configuration like this:

Before:

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

Now with this approach spring and hibernate can find the 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>  

Now if hibernate can´t find the jar, it will show you a full path in the log to where it was spectating for the library, in my case i corrected my web application´s persistence.xml to:

<?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>

I´ll check the Spring´s source code and see why that happens!