spring - 当实体由 Spring 管理而没有 persistence.xml 时,如何使用 EclipseLink 静态编织 JPA 实体

标签 spring spring-data-jpa eclipselink compile-time-weaving

我有一个基于 Spring 的项目,因此实体管理器是通过编程方式设置的,不需要 persistence.xml 文件来列出所有实体。

我目前正在使用加载时编织,但正在尝试使用 Eclipselink 和 Gradle 进行静态编织。我想复制 Maven eclipselink 插件执行的操作:

https://github.com/ethlo/eclipselink-maven-plugin

我设置了以下 gradle(请注意,它是 Kotlin DSL,而不是 groovy):

task<JavaExec>("performJPAWeaving") {


    val compileJava: JavaCompile = tasks.getByName("compileJava") as JavaCompile
    dependsOn(compileJava)

    val destinationDir = compileJava.destinationDir

    println("Statically weaving classes in $destinationDir")
    inputs.dir(destinationDir)
    outputs.dir(destinationDir)
    main = "org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
    args = listOf("-persistenceinfo", "src/main/resources", destinationDir.getAbsolutePath(), destinationDir.getAbsolutePath())

    classpath = configurations.getByName("compile")


}

当我尝试运行该任务时,编织任务失败,因为它正在寻找不存在的 persistence.xml。

有什么方法可以在基于 Spring 的 JPA 项目中静态编织 JPA 实体吗?

Exception Description: An exception was thrown while processing persistence.xml from URL: file:/home/blabla/trunk/my-module/src/main/resources/
Internal Exception: java.net.MalformedURLException
        at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionProcessingPersistenceXML(PersistenceUnitLoadingException.java:117)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:579)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceArchive(PersistenceUnitProcessor.java:536)
        ... 6 more
Caused by: java.net.MalformedURLException
        at java.net.URL.<init>(URL.java:627)
        at java.net.URL.<init>(URL.java:490)
        at java.net.URL.<init>(URL.java:439)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:620)
        at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:806)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:577)
        ... 7 more
Caused by: java.lang.NullPointerException
        at java.net.URL.<init>(URL.java:532)
        ... 17 more

最佳答案

根据org.eclipse.persistence.tools.weaving.jpa.StaticWeave文档中,它需要 persistence.xml 来生成静态编织源。

Usage:
StaticWeave [options] source target

Options:
-classpath Set the user class path, use ";" as the delimiter in Window system and ":" in Unix system.

-log The path of log file, the standard output will be the default.

-loglevel Specify a literal value for eclipselink log level(OFF,SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST). The default value is OFF.

-persistenceinfo The path contains META-INF/persistence.xml. This is ONLY required when the source does not include it. The classpath must contain all the classes necessary in order to perform weaving.

我使用 eclipselink maven 插件运行 maven 构建,它可以在没有 persistence.xml 的情况下工作,正如您提到的,因为它生成 persistence.xml 在调用 StaticWeave.class 之前,当它不在 CLASSPATH 中时,使用此方法。

 private void processPersistenceXml(ClassLoader classLoader, Set<String> entityClasses)
    {
        final File targetFile = new File(this.persistenceInfoLocation + "/META-INF/persistence.xml");
        getLog().info("persistence.xml location: " + targetFile);

        final String name = project.getArtifactId();
        final Document doc = targetFile.exists() ? PersistenceXmlHelper.parseXml(targetFile) : PersistenceXmlHelper.createXml(name);

        checkExisting(targetFile, classLoader, doc, entityClasses);

        PersistenceXmlHelper.appendClasses(doc, entityClasses);
        PersistenceXmlHelper.outputXml(doc, targetFile);
    }

完整源代码为here

我相信您可以在 gradle 构建中遵循相同的方法。

关于spring - 当实体由 Spring 管理而没有 persistence.xml 时,如何使用 EclipseLink 静态编织 JPA 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55161549/

相关文章:

spring - spring 的默认 Autowiring 是什么?

java - 如何将嵌套 JSON 数组转换为 java 对象?

java - JPA双向关系

java - JPA 与 EclipseLink : Decoding charset issue

from子句中的jpa eclipselink子查询

java - 使用 JSP 表单更新对象

java - JBoss - Spring Web 项目 - 需要在 URL 中包含 jsessionid 以在 JBoss 集群解决方案中保持粘性

java - 如何在返回 String 的 Spring MVC @ResponseBody 方法中响应 HTTP 400 错误

java - Spring Data JPA 使用多个数据库时,No bean named "ConfigurationClassPostProcessor.importRegistry"发生异常

spring - 无法使用 JPA 截断 PostgreSQL 表