简体   繁体   English

如何指定在生成的persistence.xml中使用的持久性单元名称?

[英]How do I specify the persistence unit name to be used in the generated persistence.xml?

We make extensive use of Hyperjaxb3 (version 0.6.2) to create and customize our JPA entity classes. 我们广泛使用Hyperjaxb3(版本0.6.2)来创建和定制JPA实体类。

One minor annoyance is the fact that as we add to our schema, the generated persistence unit name sometimes changes. 一个小烦人的事实是,当我们添加到架构中时,生成的持久性单元名称有时会更改。 In particular, this happens when we 1) change the XML namespaces in an included XSD file, 2) remove a namespace from the compilation, or 3) add a namespace to the compilation. 特别是,当我们1)更改包含的XSD文件中的XML名称空间,2)从编译中删除名称空间或3)将名称空间添加到编译中时,就会发生这种情况。

It appears that the generate namespace is the concatenation of the (massaged) namespaces that are included in the compilation, so it makes sense that if we tweak the namespaces that the generated unit name would change. 看起来,generate名称空间是编译中包含的(massaged)名称空间的串联,因此有意义的是,如果我们调整名称空间,则生成的单元名称将更改。

The problem is that we need to include the persistence unit name in some of our other configuration files across several modules. 问题是,我们需要在几个模块中的一些其他配置文件中包含持久性单元名称。 Every time the persistence unit name changes we have that many more files to update. 每次持久性单元名称更改时,我们都有更多文件要更新。

We would prefer to be able to specify the persistence unit name when xjc is invoked (in our maven build) or perhaps in global bindings. 我们希望能够在调用xjc时(在我们的maven构建中)或在全局绑定中指定持久性单元名称。

How do I specify the persistence unit name to be used in the generated persistence.xml? 如何指定在生成的persistence.xml中使用的持久性单元名称?

We have not found a real solution to this problem. 我们尚未找到解决此问题的真正方法。 Until we do, we are using the following workaround. 在我们这样做之前,我们将使用以下解决方法。

In our pom.xml, after xjc code generation, we are post-processing to replace the persistence unit name in the generated persistence.xml. 在我们的pom.xml中,在生成xjc代码之后,我们将进行后期处理以替换生成的persistence.xml中的持久性单元名称。 In this example, we are naming the persistence unit based on the maven module name. 在此示例中,我们基于Maven模块名称命名持久性单元。

  <plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <executions>
      <execution>
        <id>Name Persistence Unit</id>
        <phase>process-resources</phase>
        <goals>
          <goal>replace</goal>
        </goals>
        <configuration>
          <includes>
            <include>${basedir}/target/generated-sources/xjc/**/persistence.xml</include>
          </includes>
          <regex>true</regex>
          <replacements>
            <replacement>
              <token>persistence-unit name=".*"</token>
              <value>persistence-unit name="${project.artifactId}"</value>
            </replacement>
          </replacements>
        </configuration>
      </execution>
    </executions>
  </plugin>

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

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