Skip to content

Adding_vendor specific_annotations

Alexey Valikov edited this page Aug 31, 2017 · 1 revision

Adding vendor-specific annotations

Adding vendor-specific annotations

Hyperjaxb3 does not generate any vendor-specific annotations or mappings, only standard JPA. The main reason for this is that vendor-specific support is extremely effort-consuming, I can't handle such efforts in the frame of a non-commercial product.

Icon If you have a high-profile project which need vendor-specific extensions and ready to invest in development, you may get in contact with me.

Having stated that, there is still a solution which allows you adding vendor-specific annotations to the generated classes. This solution is based on the Annotate plugin which I've developed in the frame of the JAXB2 Basics project.

Here's a short overview of this solution:

  • Include the annotation library into your classpath.
  • Enable the Annotate plugin in your build.
  • Add annox:annotate customization elements.

Basically, you let Hyperjaxb3 generate standard JPA annotations and add non-standard (verndor-specific) things via customizations.

Hibernate Annotations example

I'll demonstrate it by the example of Hibernate Annotations.

Assume we want to add a DELETE_ORPHAN cascade to the one-to-many property:

@OneToMany(targetEntity = Foo.class, cascade = {       
    javax.persistence.CascadeType.ALL                  
})                                                     
@org.hibernate.annotations.Cascade({                                             
    org.hibernate.annotations.CascadeType.DELETE_ORPHAN
})
public List<Foo> getFoos() {...}

The @OneToMany is a standard JPA annotation which is generated by Hyperjaxb3. However @org.hibernate.annotations.Cascade is Hibernate-specific annotation which is not handled by Hyperjaxb3. We'll need the Annotate plugin for this.

Including the annotation library to the classpath

First of all you must make the annotation library (Hibernate Annotations in this case) available by adding it to the dependencies of the maven-hyperjaxb3-plugin:

<plugin>
    <groupId>org.jvnet.hyperjaxb3</groupId>
    <artifactId>maven-hyperjaxb3-plugin</artifactId>
    <configuration>
        ...
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.4.0.GA</version>
        </dependency>
    </dependencies>
</plugin>

Turning on the Annotate plugin

Next we'll need to turn on the Annotate plugin:

<plugin>
    <groupId>org.jvnet.hyperjaxb3</groupId>
    <artifactId>maven-hyperjaxb3-plugin</artifactId>
    <configuration>
        ...
        <args>
            <arg>-Xannotate</arg>
        </args>
    </configuration>
    <dependencies>...</dependencies>
</plugin>

Adding customization elements

Now we're ready to customize:

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"

    xmlns:hj="http://hyperjaxb3.jvnet.org/ejb/schemas/customizations"
    xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"

    xmlns:annox="http://annox.dev.java.net"
    xmlns:ha="http://annox.dev.java.net/org.hibernate.annotations"

    jaxb:extensionBindingPrefixes="hj orm xjc annox"
    jaxb:version="2.1" ... >

    <!-- ... -->

    <xs:complexType name="...">
        <xs:sequence>
            <xs:element name="foo" type="test:foo" minOccurs="0" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate>
                            <ha:Cascade value="DELETE_ORPHAN"/>
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
            <!-- ... -->
        </xs:sequence>
    </xs:complexType>

    <!-- ... -->

</xs:schema>

Icon Please see the Annotate plugin documentation and Annox user guide for more information on formulation your Java annotations in XML.

Icon See the cu-one test project for the working example.

Clone this wiki locally