我有一个基于 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 targetOptions:
-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/