how to generate @TABLE tags using jaxb2 maven plugin

517 views Asked by At

Hi I have followed some tutorials and written a schema which is as follows :

<xs:element name="User" type="kmcs:User"/>
    <xs:element name="UserList" type="kmcs:UserList"/>

    <xs:complexType name="User">
        <xs:sequence>
                <xs:element name="id" type="xs:long" minOccurs="0" />
                <xs:element name="name" type="xs:string" />
                <xs:element name="registrationDate" type="xs:dateTime" />
            </xs:sequence>
            <xs:attribute name = ""/>

    </xs:complexType>

    <xs:complexType name="UserList">
        <xs:complexContent>
            <xs:extension base="kmcs:User">
                <xs:sequence>
                   <xs:element name="user"  minOccurs="0"
                maxOccurs="unbounded" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>


</xs:schema>

It generates the following :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", propOrder = {
    "id",
    "name",
    "registrationDate"
})
@XmlSeeAlso({
    UserList.class
})
@Entity
@XmlRootElement(name = "{@id}")
@Table(schema = "schemaname", uniqueConstraints = {

}, name = "BillingAccountNumberDetailsInfo")
public class User
    implements Serializable, ToString
{

I wanted something like this :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", propOrder = {
    "id",
    "name",
    "registrationDate"
})
@XmlSeeAlso({
    UserList.class
})
@Entity
@XmlRootElement(name ="user")
@Table(schema = "schema_name", uniqueConstraints = {

}, name = "User")
public class User
    implements Serializable, ToString
{

So what I wanted is basically the table name which it picks whatever I provide as hard coded but I wanted it to show the class name or class object name .

1

There are 1 answers

0
Xstian On

Use this configuration.

    <bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:annox="http://annox.dev.java.net"
        xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
        <bindings schemaLocation="../yourSchema.xsd">

            <bindings node="//xs:complexType[@name='User']">
                <annox:annotate>
                    <annox:annotate annox:class="javax.persistence.Entity">
                    </annox:annotate>
                    <annox:annotate annox:class="javax.persistence.Table" schema = "schema_name" name = "User">
                    </annox:annotate>
                    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="User">
                    </annox:annotate>
                </annox:annotate>
            </bindings>

        </bindings>
    </bindings>

the maven plugin configuration is

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xannotate</arg>
                        <arg>-nv</arg>
                        <arg>-Xnamespace-prefix</arg>
                    </args>
                    <extension>true</extension>
                    <schemaDirectory>src/main/resources/schema</schemaDirectory>
                    <schemaIncludes>
                        <schemaInclude>yourSchema.xsd</schemaInclude>
                    </schemaIncludes>
                    <bindingDirectory>src/main/resources/schema/xjb</bindingDirectory>
                    <bindingIncludes>
                        <include>*.xjb</include>
                    </bindingIncludes>
                    <debug>true</debug>
                    <verbose>true</verbose>
                    <episode>true</episode>
                    <forceRegenerate>true</forceRegenerate>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.0</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>0.6.0</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-namespace-prefix</artifactId>
                            <version>1.1</version>
                        </plugin>
                    </plugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.hibernate.javax.persistence</groupId>
                        <artifactId>hibernate-jpa-2.0-api</artifactId>
                        <version>1.0.1.Final</version>
                    </dependency>
                </dependencies>
            </plugin>