在eclipse中找不到persistence.xml中的jar文件 [英] jar-file from persistence.xml is not found in eclipse

查看:936
本文介绍了在eclipse中找不到persistence.xml中的jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的eclipse工作区中有两个maven项目。第一个项目有hibernate实体,应该在第二个项目中使用。我将它作为第二个项目中的依赖项添加为:

 < dependency> 
< groupId> org.prosolo< / groupId>
< artifactId> bigdata.common< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< /依赖关系>

我禁用了工作区分辨率,因此,bigdata.common-0.0.1-SNAPSHOT.jar列在Maven下依赖。此外,我配置了persistence.xml来导入这个jar文件:

 < persistence-unit name =entityManager
transaction-type =RESOURCE_LOCAL>
< jar-file> lib / bigdata.common-0.0.1-SNAPSHOT.jar< / jar-file>

但是,当我从eclipse运行项目时,由于以下错误,EntityManager无法初始化:

 引起:java.lang.IllegalArgumentException:引用了文件[lib / bigdata.common-0.0.1-SNAPSHOT.jar]通过给定的URL [file:lib / bigdata.common-0.0.1-SNAPSHOT.jar]不存在

如果我从终端运行项目,或者如果我使用.jar文件的绝对路径,但是相对路径问题在Eclipse中仍然存在,则没有问题。我使用maven-jetty-plugin在两种情况下运行应用程序。



我发现了这个错误报告 https://hibernate.atlassian.net/browse/HHH-4161 ,但似乎没有解决方案。



我想知道这个问题是否有解决方案,或者如果不能使用jar文件,我应该使用其他方法来使用其他的hibernate实体项目包扫描。



谢谢,
Zoran

解决方案

这个问题已经在Hibernate 5.0.7中解决了,请参阅 HHH-4161

Hibernate 5.0.2上也会出现这个问题,根据最近一次关于 HHH-4161



问题,我提出了一个基于自定义Scanner类的解决方法,该类可以管理相对URL并将其绝对化。我将AbstractScannerImpl源代码复制到test.MyCustomScanner中,添加了StandardScanner的构造函数,并在environment.getNonRootUrls()循环中添加了以下代码:

  if(!url.getPath()。startsWith(/)){
// relative URL =>使其相对于META-INF / persistence.xml的根URL进行处理
//这意味着rootUrl具有META-INF目录(但rootUrl在路径中没有META-INF)。
//请参阅JPA 2.1规范第366-368页:http://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf
//注意:虽然解决方案看起来很聪明,但getNonRootUrls可能包含比< jar-file>更多的
//。因此,它可能有副作用。
尝试{
网址absoluteUrl =新网址(environment.getRootUrl(),url.getPath());
url = absoluteUrl;
} catch(MalformedURLException e){
抛出新的RuntimeException(无法使相对URL为绝对:+ url);




$ b $ p
$ b

精神与Lauri Harpf的拉动请求( https://github.com/hibernate/hibernate-orm/pull/889/files ),但优先考虑相对URL并使用相对URL测试而不是异常捕获机制。



这种方法的风险在于所有非根URL都以相同的方式进行管理,而不仅仅是< jar-file> 标签。因此,可能会出现副作用。
自定义扫描器随后被添加到persistence.xml中:

 < property name =hibernate.ejb。 resource_scannervalue =test.MyCustomScanner/> 

运行时,执行日志不会显示任何异常,只会引起关注 MyEntity 表不存在,这表明entities.jar已被正确处理:

  oct 。 30,2015 8:11:50 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation 
INFO:HHH000204:正在处理PersistenceUnitInfo [
名称:test-hibernate2
...]
oct。 30,2015 8:11:51 PM org.hibernate.Version logVersion
INFO:HHH000412:Hibernate Core {5.0.2.Final}
oct。 30,2015 8:11:51 PM org.hibernate.cfg.Environment< clinit>
INFO:HHH000206:找不到hibernate.properties
oct。 30,2015 8:11:51 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO:HHH000021:字节码提供程序名称:javassist
oct。 30,2015 8:11:51 PM org.hibernate.boot.internal.MetadataBuilderImpl $ MetadataBuildingOptionsImpl< init>
INFO:HHH90000001:找到用于指定扫描程序的已弃用设置[hibernate.ejb.resource_scanner];使用[hibernate.archive.scanner]而不是
oct。 30,2015 8:11:51 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager< clinit>
INFO:HCANN000001:Hibernate Commons Annotations {5.0.0.Final}
oct。 30,2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN:HHH000402:使用Hibernate内置连接池(不适合生产使用!)
oct 。 30,2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO:HHH000401:在URL [jdbc:hsqldb:mem:testdb中使用驱动程序[org.hsqldb.jdbcDriver] ]
oct。 30,2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO:HHH000046:连接属性:{user = sa}
oct。 30,2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO:HHH000006:Autocommit mode:false
oct。 30,2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO:HHH000115:Hibernate连接池大小:20(min = 1)
oct。 30,2015 8:11:51 PM org.hibernate.dialect.Dialect< init>
INFO:HHH000400:使用方言:org.hibernate.dialect.HSQLDialect
oct。 30,2015 8:11:52 PM org.hibernate.tool.hbm2ddl.SchemaUpdate执行
INFO:HHH000228:运行hbm2ddl模式更新
oct。 30,2015 8:11:52 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO:HHH000262:Table not found:MyEntity
oct。 30,2015 8:11:52 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO:HHH000262:Table not found:MyEntity

我在之前的客户中使用过的另一种解决方法是在< class> 标签的列表中创建根据应用程序类路径中JAR的依赖关系构建时间。它看起来有点复杂,但它对应用程序提供了非常好的控制:开发人员可以查看persistence.xml,以立即知道哪些实体类将在运行时加载。请注意,在我想到定制扫描器解决方法之前,这种方法已在2007年使用。


I have two maven projects in my eclipse workspace. First project has hibernate entities which should be used in the second project. I added it as dependency in the second project as:

<dependency>
        <groupId>org.prosolo</groupId>
        <artifactId>bigdata.common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

I disabled workspace resolution so, bigdata.common-0.0.1-SNAPSHOT.jar is listed under Maven Dependencies. Furthermore, I configured persistence.xml to import this jar file:

<persistence-unit name="entityManager"
    transaction-type="RESOURCE_LOCAL">      
    <jar-file>lib/bigdata.common-0.0.1-SNAPSHOT.jar</jar-file>

However, when I run project from eclipse, EntityManager can't be initialized due to the following error:

Caused by: java.lang.IllegalArgumentException: File [lib/bigdata.common-0.0.1-SNAPSHOT.jar] referenced by given URL [file:lib/bigdata.common-0.0.1-SNAPSHOT.jar] does not exist

There is no problem if I run project from terminal or if I use absolute path to .jar file, but with relative path problem persists in Eclipse. I'm using maven-jetty-plugin to run application in both cases.

I've found this bug report https://hibernate.atlassian.net/browse/HHH-4161, but it seems that there is no solution for it.

I'm wondering if there is any solution for this problem, or if it's not possible to use jar-file, what other approach should I use to be able to use hibernate entities from other project with package scanning.

Thanks, Zoran

解决方案

[EDIT] This issue has been solved in Hibernate 5.0.7, see HHH-4161.

This issue also occurs on Hibernate 5.0.2 and is planned to be solved in Hibernate 5.0.5, according to recent update on HHH-4161.

In this issue, I proposed a workaround based on a custom Scanner class which manages the relative URLs and absolutize them. I copied the AbstractScannerImpl source code into test.MyCustomScanner, added the constructors of StandardScanner, and added the following code in the environment.getNonRootUrls() loop:

if (!url.getPath().startsWith("/")) {
    // relative URL => make it processed relatively to the root URL of the META-INF/persistence.xml
    // This means that rootUrl has a META-INF directory (but the rootUrl has no META-INF in path).
    // See JPA 2.1 specification page 366-368: http://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf
    // Note: while the solution looks smart, the getNonRootUrls may include more
    // than the <jar-file>. Thus, it may have side effects.
    try {
        URL absoluteUrl = new URL(environment.getRootUrl(),url.getPath());
        url=absoluteUrl;
    } catch (MalformedURLException e) {
        throw new RuntimeException("cannot make the relative URL as absolute:"+url);
    }
}

The spirit is the same as Lauri Harpf's pull request (https://github.com/hibernate/hibernate-orm/pull/889/files), but with the priority given to the relative URL and using a relative URL test instead of an exception catching mechanism.

The risk with this approach is that all non root URLs are managed the same way, not only the ones of <jar-file> tags. Thus, side effects may appear. The custom scanner is then added to the persistence.xml:

<property name="hibernate.ejb.resource_scanner" value="test.MyCustomScanner" />

When run, the execution log shows no exception and only raises a concern that MyEntity table does not exist, which shows that the entities.jar has been processed correctly:

oct. 30, 2015 8:11:50 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: test-hibernate2
    ...]
oct. 30, 2015 8:11:51 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.2.Final}
oct. 30, 2015 8:11:51 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
oct. 30, 2015 8:11:51 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
oct. 30, 2015 8:11:51 PM org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl <init>
INFO: HHH90000001: Found usage of deprecated setting for specifying Scanner [hibernate.ejb.resource_scanner]; use [hibernate.archive.scanner] instead
oct. 30, 2015 8:11:51 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
oct. 30, 2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
oct. 30, 2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.hsqldb.jdbcDriver] at URL [jdbc:hsqldb:mem:testdb]
oct. 30, 2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=sa}
oct. 30, 2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
oct. 30, 2015 8:11:51 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
oct. 30, 2015 8:11:51 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
oct. 30, 2015 8:11:52 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
oct. 30, 2015 8:11:52 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: MyEntity
oct. 30, 2015 8:11:52 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: MyEntity

Another workaround I used in one of my previous customer was to build the list of <class> tags at build time based on the dependencies of the JARs in the application classpath. It looks a bit complex at first sight, but it gives a very good control on the application: developers can look in the persistence.xml to immediately know which entity classes will be loaded at runtime. Note that this approach was used in 2007, long before I thought about the custom Scanner workaround.

这篇关于在eclipse中找不到persistence.xml中的jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆