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.
- EclipseLink JPA2.1 jar-file多项目使用说明
- tomcat 下多项目共享jar包
- tomcat配置多项目JAR包共享
- jpa2
- Tomat之——多项目jar包共享配置
- Tomcat下多项目jar包共享配置
- tomcat中设置多项目共享jar;类包
- Tomcat打包时多项目共享jar和精确指定jar版本
- jar file
- 多项目相互依赖的Java程序打包并引入外部Jar包方式
- 查找maven依赖的jar mvn dependency:tree解决多项目多引入jar包版本导致的问题
- commons-fileupload.jar 使用说明
- Spring3.1+SpringMVC3.1+JPA2.0
- JPA2.1与Java8 date/time
- 多项目共享Session
- 多项目管理
- 多项目管理
- 多项目管理
- java基础配置安装
- 常用的正则表达式
- Android_6.0 ROM(Kernel + Android)编译 ERROR总结
- 安卓textview编辑部分字体颜色
- Linux tcpdump命令详解
- EclipseLink JPA2.1 jar-file多项目使用说明
- mysql 的基础
- C++直接初始化和复制初始化
- 不能将“this”指针从“const CPaopao”转换为“CPaopao &
- SGE:作业调度系统安装和使用简要说明
- Anaconda安装tensorflow后,spyder无法调用,终端可以。
- Hibernate命名策略自动改变表名或字段名的问题
- 中文排序
- Codeforces Round #410 (Div.2) C.Mike and gcd problem-数学