简体   繁体   English

在eclipse中找不到来自persistence.xml的jar文件

[英]jar-file from persistence.xml is not found in eclipse

I have two maven projects in my eclipse workspace. 我的Eclipse工作区中有两个Maven项目。 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. 我禁用了工作空间解析,因此在Maven依赖项下列出了bigdata.common-0.0.1-SNAPSHOT.jar。 Furthermore, I configured persistence.xml to import this jar file: 此外,我将persistence.xml配置为导入此jar文件:

<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: 但是,当我从Eclipse运行项目时,由于以下错误,无法初始化EntityManager:

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. 如果从终端运行项目或使用.jar文件的绝对路径都没有问题,但是相对路径问题在Eclipse中仍然存在。 I'm using maven-jetty-plugin to run application in both cases. 在这两种情况下,我都使用maven-jetty-plugin运行应用程序。

I've found this bug report https://hibernate.atlassian.net/browse/HHH-4161 , but it seems that there is no solution for it. 我发现此错误报告https://hibernate.atlassian.net/browse/HHH-4161 ,但似乎没有解决方案。

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. 我想知道是否有针对此问题的任何解决方案,或者如果无法使用jar文件,我应该使用哪种其他方法才能将其他项目中的休眠实体与程序包扫描一起使用。

Thanks, Zoran 谢谢Zoran

[EDIT] This issue has been solved in Hibernate 5.0.7, see HHH-4161 . [编辑] Hibernate 5.0.7中已解决此问题,请参见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 . 根据HHH-4161的最新更新,此问题也发生在Hibernate 5.0.2上,并计划在Hibernate 5.0.5中解决。

In this issue, I proposed a workaround based on a custom Scanner class which manages the relative URLs and absolutize them. 在本期中, 我提出了一个基于自定义Scanner类的变通方法 ,该类管理相对URL并对其进行绝对化。 I copied the AbstractScannerImpl source code into test.MyCustomScanner, added the constructors of StandardScanner, and added the following code in the environment.getNonRootUrls() loop: 我将AbstractScannerImpl源代码复制到test.MyCustomScanner中,添加了StandardScanner的构造函数,并在environment.getNonRootUrls()循环中添加了以下代码:

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. 其精神与Lauri Harpf的pull请求( https://github.com/hibernate/hibernate-orm/pull/889/files )相同,但具有相对URL优先级,并使用相对URL测试而不是异常捕获机制。

The risk with this approach is that all non root URLs are managed the same way, not only the ones of <jar-file> tags. 这种方法的风险在于,所有非根URL都以相同的方式进行管理,而不仅仅是<jar-file>标记。 Thus, side effects may appear. 因此,可能出现副作用。 The custom scanner is then added to the persistence.xml: 然后将自定义扫描程序添加到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: 运行时,执行日志不会显示任何异常,只会引起对MyEntity表不存在的担忧,这表明对Entitys.jar的处理正确:

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. 我在以前的一位客户中使用的另一个解决方法是,在构建时根据应用程序类路径中JAR的依赖关系来构建<class>标记列表。 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. 乍一看似乎有些复杂,但是它对应用程序提供了很好的控制:开发人员可以查看persistence.xml来立即知道在运行时将加载哪些实体类。 Note that this approach was used in 2007, long before I thought about the custom Scanner workaround. 请注意,早在我想到自定义扫描仪解决方法之前,这种方法就已在2007年使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM