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

时间:2015-09-01 14:58:00

标签: maven jpa xsd jaxb hyperjaxb

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

此致 Erzen

pom.xml片段

try:
    assert(x in range(0, 20))
except AssertionError:
    print("variable x is out of range")
    exit()

bindings-xjc.xjb摘录

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

4 个答案:

答案 0 :(得分:2)

我不确定这是否可行,但是尝试一下这个元素,也许它有一个'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

答案 1 :(得分:2)

Author of here.

See @Stefan's answer, just add the schema="schema_name" attribute:

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

orm:table is actually a JPA XML element so that's documented in the JPA spec. :)

See this schema:

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

I'm basically not inventing anything here.

You don't need JAXB2 Annotate Plugin for that, this works OOTB.

Here's an issue for the global prefix:

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

Unresolved yet. Can be solved via custom naming now, but that's quite awkward.

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

I agree, it would be nice to make it configurable.

Update How to do this globally:

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

But you'll also need to customize defaults for associations and so on. See he built-in defaults here:

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

答案 2 :(得分:2)

@lexicore Thnx的帮助。在将您的建议放在正确的背景下之后,它就起作用了。

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

答案 3 :(得分:0)

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

的persistence.xml:

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

META-INF文件夹中的orm.xml:

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