简体   繁体   中英

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.

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.

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.

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.

How do I specify the persistence unit name to be used in the generated 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. In this example, we are naming the persistence unit based on the maven module name.

  <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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