在外部 jaxb-Binding 中编辑 @java.persitence.Table

Editing @java.persitence.Table in external jaxb-Binding

我已经建立了一个 Maven 项目来从 xsd-Schema 生成 Java 类。首先,我配置了 maven-hyperjaxb3-plugin(请参阅下面的 pom.xml 片段),以便它可以将默认的 JPA2 注释放入实体中。其中一个注解是@java.persitence.Table(name = "table_name")。我想通过外部全局绑定扩展此注释,以便我也可以将模式名称放入其中。这样我就会得到@java.persitence.Table(name = "table_name", schema = "schema_name")。有没有办法做到这一点? 在 table 的名称中全局添加一个前缀怎么样:@java.persitence.Table(name = "prefix_table_name"),有什么办法吗?

问候 尔岑

pom.xml 片段

<groupId>org.jvnet.hyperjaxb3</groupId>
<artifactId>maven-hyperjaxb3-plugin</artifactId>
<version>0.6.0</version>
<executions>
    <execution>
        <goals>
            <goal>generate</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <variant>jpa2</variant>
    <extension>true</extension>
    <roundtripTestClassName>EKMSEnvelopeRoundtripTest</roundtripTestClassName>
    <args>
        <arg>-Xinheritance</arg>
        <arg>-XtoString</arg>
        <arg>-Xannotate</arg>
    </args>
    <schemaExcludes>
        <exclude>ekvaattributes.xsd</exclude>
    </schemaExcludes>
</configuration>

绑定-xjc.xjb 片段

<jaxb:globalBindings localScoping="toplevel">
    <!-- JPA-entities must be serializable -->
    <xjc:serializable />
</jaxb:globalBindings>


<jaxb:bindings schemaLocation="schema.xsd"
    node="/xs:schema">

    <annox:annotate>
        <!-- my attempt -->
        <annox:annotate annox:class="javax.persistence.Table"
            schema="schema_name">
        </annox:annotate>
    </annox:annotate>

    <hj:persistence>
        <hj:default-generated-id name="Hjid">
            <orm:generated-value strategy="IDENTITY" />
        </hj:default-generated-id>
    </hj:persistence>
</jaxb:bindings>

我不确定这是否可行,但试试这个元素,也许它有一个 'schema' 属性,遗憾的是它没有很好的记录。

此致, 斯特凡

<jaxb:bindings schemaLocation="schema.xsd"
node="/xs:schema">
    <annox:annotate>

    <hj:persistence>
        <hj:default-generated-id name="Hjid">
            <orm:generated-value strategy="IDENTITY" />
        </hj:default-generated-id>
    </hj:persistence>

    <!-- try this -->
    <hj:entity>
        <orm:table name="item"/>
    </hj:entity>
</jaxb:bindings>

来源:http://confluence.highsource.org/display/HJ3/Customization+Guide

这里是 的作者。

参见@Stefan 的回答,只需添加 schema="schema_name" 属性:

<orm:table name="item" schema="schema_name"/>

orm:table 实际上是一个 JPA XML 元素,因此在 JPA 规范中有记录。 :)

查看此架构:

https://github.com/highsource/hyperjaxb3/blob/master/ejb/schemas/persistence/src/main/resources/persistence/orm/orm_1_0.xsd#L1814-L1815

我基本上没有在这里发明任何东西。

你不需要 JAXB2 Annotate Plugin,这适用于 OOTB。

这是全局前缀的问题:

http://jira.highsource.org/browse/HJIII-87

尚未解决。现在可以通过自定义命名来解决,但是很尴尬。

https://github.com/highsource/hyperjaxb3/tree/master/ejb/tests/custom-naming

我同意,让它可配置就好了。

更新 如何全局执行此操作:

<hj:default-entity>
    <orm:table name="item" schema="schema_name"/>
</hj:default-entity>

但是您还需要自定义关联等的默认设置。在此处查看内置默认值:

https://github.com/highsource/hyperjaxb3/blob/master/ejb/plugin/src/main/resources/org/jvnet/hyperjaxb3/ejb/strategy/customizing/impl/DefaultCustomizations.xml

您还可以在从 persistence.xml 引用的 orm 文件中全局定义所有实体的模式。无需将架构复制到每个 @Table 注释中。

persistence.xml:

...
  <persistence-unit name="MySchemaPU"  transaction-type="JTA">
     <mapping-file>META-INF/orm.xml</mapping-file>

还有一个 orm.xml 在 META-INF 文件夹中:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
                 version="1.0">
 <persistence-unit-metadata>

  <persistence-unit-defaults>
   <schema>schema_name</schema>
  </persistence-unit-defaults>
 </persistence-unit-metadata>
</entity-mappings

@lexicore 感谢您的帮助。在将您的建议放在正确的上下文中后,它奏效了。

    <hj:persistence>
        <hj:default-entity>
            <!-- no need to overwrite the default generated table names-->
            <orm:table schema="schema_name" />
        </hj:default-entity>
    </hj:persistence>