EclipseLink JPA2.1 jar-file多项目使用说明

来源:互联网 发布:网络创业项目 编辑:程序博客网 时间:2024/05/29 06:38

如果项目是分模块和分层开发,例如:
core模块,包含@Entity注解的类
order模块,包含@Entity注解的类,但同时依赖core模块.

怎么办?

为了项目模块的集成方便,实现灵活.在模块的设计中只有实体类和接口,如果一个项目定制需要哪些模块,只需要将模块加入依赖同时提供接口实现即可,喜欢ssh可以用ssh来实现接口,喜欢ssm可以用ssm来实现接口.

如果项目中使用jpa,模块接口的实现者拿到jar,只需要在persistence.xml中用jar-file引入jar即可.

示例项目结构:
core模块,包含@Entity注解的类,业务层和持久层接口
portal项目,依赖core模块.项目的pom.xml

        <dependency>            <groupId>net.htage</groupId>            <artifactId>core</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>

jar-file使用

core模块的persistence.xml,这里不要包含特定化的数据库厂商

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">  <persistence-unit name="corePU" transaction-type="RESOURCE_LOCAL">    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>    <exclude-unlisted-classes>false</exclude-unlisted-classes>    <shared-cache-mode>ALL</shared-cache-mode>  </persistence-unit></persistence>

portal项目的persistence.xml

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">  <persistence-unit name="portalPU" transaction-type="RESOURCE_LOCAL">    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>    <jar-file>lib\core-1.0-SNAPSHOT.jar</jar-file>    <exclude-unlisted-classes>false</exclude-unlisted-classes>    <shared-cache-mode>NONE</shared-cache-mode>    <properties>      <property name="javax.persistence.jdbc.url" value="${jdbc.url}"/>      <property name="javax.persistence.jdbc.user" value="${jdbc.username}"/>      <property name="javax.persistence.jdbc.driver" value="${jdbc.driverClassName}"/>      <property name="javax.persistence.jdbc.password" value="${jdbc.password}"/>      <property name="javax.persistence.schema-generation.database.action" value="create"/>      <property name="eclipselink.logging.level" value="FINE"/>      <property name="eclipselink.weaving.lazy" value="false"/>    </properties>  </persistence-unit></persistence>

说明:
这里重点是jar-file的位置,跑在tomcat中的
core-1.0-SNAPSHOT.jar位于项目portal/WEB-INF/lib下,core的persistence.xml在jar包的META-INF目录中

portal的persistence.xml在/WEB-INF/classes/META-INF下.

如果位置不对可能会出现以下异常

1.一目了然(jar-file=core-1.0-SNAPSHOT.jar)

Exception Description: Predeployment of PersistenceUnit [portalPU] failed.Internal Exception: java.lang.RuntimeException: java.io.FileNotFoundException: D:\tomcat8\temp\0-portal\WEB-INF\core-1.0-SNAPSHOT.jar (系统找不到指定的文件。)    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:2035)    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:2026)    at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactoryImpl(PersistenceProvider.java:347)    at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:313)    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:290)    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1573)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1511)    ... 25 more

2.看不出是哪出错了(启动不会有异常,打开连接提示找不到实体)
这里写图片描述

Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.JPQLExceptionException Description: Problem compiling [SELECT COUNT(a) FROM Article a WHERE a.status=TRUE]. [21, 28] The abstract schema type 'Article' is unknown.[37, 45] The state field path 'a.status' cannot be resolved to a valid type.

3.无从下手(jar-file=../../lib/core-1.0-SNAPSHOT.jar)

25-Apr-2017 13:28:35.946 严重 [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: The resource path [/../../lib/core-1.0-SNAPSHOT.jar] has been normalized to [null] which is not valid    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1514)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)

最后

1.spring 的applicationContext.xml中persistenceUnitName使用项目的persistence-unit的name属性值

2.如果项目使用其它容器或服务器.多看一看文档.我看到有书中说war包的persistence.xml要在web-inf下.

3.如果不使用eclipseLink JPA遇到问题多看实现的文档(千万别到处白到处勾).本人折腾了2个多小时在官方文档中看到下面几句才搞好了.

About Persistence Unit Packaging Options

Java EE allows for persistence support in a variety of packaging configurations. You can deploy your application to the following module types:

EJB modules: you can package your entities in an EJB JAR. When defining a persistence unit in an EJB JAR, the persistence.xml file is not optional–you must create and place it in the META-INF directory of the JAR alongside the deployment descriptor, if it exists.

Web modules: you can use a WAR file to package your entities. In this case, place the persistence.xml file in the WEB-INF/classes/META-INF directory. Since the WEB-INF/classes directory is automatically on the classpath of the WAR, specify the mapping file relative to that directory.

Persistence archives: a persistence archive is a JAR that contains a persistence.xml file in its META-INF directory and the managed classes for the persistence unit defined by the persistence.xml file. Use a persistence archive if you want to allow multiple components in different Java EE modules to share or access a persistence unit.

Once you create a persistence archive, you can place it in either the root or the application library directory of the EAR. Alternatively, you can place the persistence archive in the WEB-INF/lib directory of a WAR. This will make the persistence unit accessible only to the classes inside the WAR, but it enables the decoupling of the definition of the persistence unit from the web archive itself.

For more information, see “Persistence Unit Packaging” in the JPA Specification.

1 0