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

2019-04-14 01:19发布

问题:

I've set up a maven project to generate Java classes from a xsd-Schema. Firstly I configured the maven-hyperjaxb3-plugin (see the pom.xml snippet below), so that it can put the default JPA2 annotations in the entities. One of this annotations is @java.persitence.Table(name = "table_name"). I want to extend this annotation through an external global binding so that I can put the name of schema in it too. So that I would get @java.persitence.Table(name = "table_name", schema = "schema_name"). Is there a way to do this? What about globally putting a prefix in the name of the table: @java.persitence.Table(name = "prefix_table_name"), any ideas how to do that?

Regards Erzen

pom.xml snippet

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

bindings-xjc.xjb snippet

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

回答1:

I'm not sure if this is possible, but try the element, maybe it has a 'schema' attribute, sadly it's not that well documented.

Regards, Stefan

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

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



回答2:

Author of hyperjaxb3 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



回答3:

@lexicore Thnx for the help. After putting your suggestion in the right context it worked.

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


回答4:

You may also define a schema for all entities globally in orm file referenced from persistence.xml. There is no need to copy schema into every @Table annotation.

persistence.xml:

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

And an orm.xml in META-INF folder:

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