javahibernatejpajaxbhyperjaxb

hyperjaxb extra underscore(_) generated in @column for some single character columns


I am trying to generate a JPA annotated java class using hyperjaxb, but have hit one problem. Any suggestion welcome :-

partial ..Pom.xml

   <!-- hyperjaxb -->
             <dependency>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-runtime</artifactId>
                <version>0.6.4</version>
            </dependency>
            <dependency>
                <groupId>org.jvnet.hyperjaxb3</groupId>
                <artifactId>maven-hyperjaxb3-plugin</artifactId>
                <version>0.5.6</version>
        </dependency>
...

        <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jvnet.hyperjaxb3</groupId>
                <artifactId>maven-hyperjaxb3-plugin</artifactId>
                 <version>0.5.6</version>
                 <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
                 <executions>
                    <execution>
                    <id>1</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                              <forceRegenerate>true</forceRegenerate>
                            <schemaDirectory>${basedir}/src/main/resources/schemas/demo</schemaDirectory>
                            <schemaIncludes>
                                <schemaInclude>demo.xsd</schemaInclude> 
                            </schemaIncludes>                               
                            <generatePackage>com.fsi.demo</generatePackage>
                             <strict>true</strict>
                            <extension>true</extension>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Here is demo.xsd:-

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="demo">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="playerID" />
                <xs:element ref="G"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="playerID" type="xs:string" />
    <xs:element name="G" type="xs:string" />
</xs:schema>

Here is the generated java class

public class Demo
    implements Equals, HashCode
{
 @XmlElement(required = true)
    protected String playerID;
    @XmlElement(name = "G", required = true)
    protected String g;
    @XmlAttribute(name = "Hjid")
    protected Long hjid;
....
... 
/**
     * Gets the value of the g property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    @Basic
    @Column(name = "G_", length = 255)
    public String getG() {
        return g;
    }

    /**
     * Sets the value of the g property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setG(String value) {
        this.g = value;
    }
}

The Extra underscore @Column(name = "G_", length = 255) is breaking my code as hibernate complains about invalid column mapping.

What I tried so far, which had no effect on the problem:- 1) Inline custom binding in demo.xsd

<xs:annotation>
            <xs:appinfo>
                <jxb:property name="G" />
            </xs:appinfo>
        </xs:annotation>

and

<xs:annotation>
            <xs:appinfo>
                <orm:attribute-override name="G">
                    <orm:column name="G" />
                </orm:attribute-override>
            </xs:appinfo>
        </xs:annotation>

what am I missing here, anyone please!

UPDATE: Hibernate Query below:- Hibernate:

select demo0_.PLAYERID as PLAYERID1_0_, demo0_.G_ as G_2_0_ from DEMO demo0_ where demo0_.PLAYERID in ( ? )

Trace:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'demo0_.G_' in 'field list'
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

and rightly so as the actual column is G and NOT G_ as generated by hyperjaxb, changing it to G (manually) solves this issue


Solution

  • From my experience, Hyperjaxb will add an underscore to the end of a column (and I suspect a table?) name, if the name of that column is a reserved word in any SQL dialect.

    E.g. We're using SQL Server, and our XML source has an attribute called VALUE. When that gets created as a column in the database, it is called VALUE_

    VALUE isn't actually a reserved word in T-SQL (SQL Server), but it is in Pl/SQL (Oracle). I would hazard a guess that NAME is a reserved word in a SQL dialect somewhere!

    I've overridden the column names in the bindings file to get around this :)

    Hope that helps!

    Edited to include examples of bindings cutomization:

    Here's an example where I rename a table, as it has an underscore at the end:

    <!-- Make table name 'INSTANCE' rather than 'INSTANCE_' -->
            <jaxb:bindings node="xs:element[@name='Instance']//xs:complexType">
                <hj:entity>
                    <orm:table name="INSTANCE" />
                </hj:entity>
            </jaxb:bindings>
    

    Here's an example for renaming a column:

    <!-- Make PeriodStart and PeriodEnd columns have the right name (instead of PeriodEndItem -->
            <jaxb:bindings node="xs:element[@name='InstancePeriod']//xs:complexType//xs:sequence//xs:element[@ref='PERIODEND']">
                <hj:basic>
                    <orm:column name="PERIODEND" />
                </hj:basic>
            </jaxb:bindings>