对于某些单字符列,@ column中生成的hyperjaxb额外下划线(_)

时间:2016-08-01 13:18:28

标签: java hibernate jpa jaxb hyperjaxb

我正在尝试使用hyperjaxb生成JPA带注释的java类,但遇到了一个问题。欢迎任何建议: -

部分..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>

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

这是生成的java类

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

额外的下划线 @Column(name =“G_”,length = 255) 因为hibernate抱怨无效列映射而破坏了我的代码。

到目前为止我尝试了什么,这对问题没有影响: - 1)demo.xsd中的内联自定义绑定

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

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

我在这里想念的是什么,有人请!

更新: 下面的Hibernate查询: - 休眠:

选择     demo0_.PLAYERID为PLAYERID1_0_,     demo0_.G_为G_2_0_ 从     DEMO demo0_ 哪里     demo0_.PLAYERID in(         ?     )

跟踪:

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)

并正确地将实际列 G 而非 G _ 由hyperjaxb生成,将其更改为G(手动)可以解决此问题

1 个答案:

答案 0 :(得分:0)

根据我的经验,如果该列的名称是任何SQL方言中的保留字,Hyperjaxb会在列的末尾添加下划线(我怀疑是一个表?)名称。

E.g。我们使用的是SQL Server,我们的XML源有一个名为VALUE的属性。当它作为数据库中的列创建时,它被称为VALUE _

VALUE实际上并不是T-SQL(SQL Server)中的保留字,但它位于Pl / SQL(Oracle)中。我猜想NAME是某个SQL方言中的保留字!

我已经覆盖了绑定文件中的列名,以解决这个问题:)

希望有所帮助!

编辑包含绑定切割的示例:

这是我重命名表的示例,因为它在末尾有一个下划线:

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

以下是重命名列的示例:

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