2016-08-01 40 views
2

我正在尝试使用hyperjaxb生成JPA注释的java类,但遇到了一个问题。任何建议欢迎: -针对某些单个字符列在@column中生成的hyperjaxb额外下划线(_)

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

我缺少什么在这里,请任何人!

UPDATE: Hibernate查询下面: - 休眠:

从 DEMO选择 demo0_.PLAYERID如PLAYERID1_0_, demo0_.G_如G_2_0_ demo0_ 其中 demo0_.PLAYERID在( ? )

微量:

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) 

这是正确的为实际列和NOT G_由hyperjaxb产生,将其更改为G(手动)解决了这个问题

+0

为什么列映射无效? – lexicore

+0

由于hyperjaxb生成的列包含多余的_(** G _ **而不是** G **)。 **注:**更新了与上述 – AshDDN

+0

更多的问题的详细信息原来的问题:XSD \t''也产生具有下划线的列名(_),像下面'@Basic @Column(名称= “名_”,长度= 255) 公共字符串的getName(){ 返回名称; }' – AshDDN

回答

0

从我的经验,Hyperjaxb将增加一个如果该列的名称是任何SQL方言中的保留字,则强调该列的末尾(并且我怀疑有一个表?)名称。

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

VALUE实际上并不是T-SQL(SQL Server)中的保留字,但它在PI/SQL(Oracle)中。我会冒险猜测NAME是SQL方言中的保留字!

我已经覆盖在绑定的列名文件来解决这个问题:)

希望帮助!

被修改为包括绑定cutomization的例子:

此处,我重命名表的例子,因为它在其端下划线:

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

感谢@pieman,这是有帮助的,我也假设,这可能不是一个问题。你可以请你分享你的示例XSD和自定义绑定,或者我需要在上面的示例中做什么? – AshDDN

+0

不用担心,@AshDDN!编辑我的答案,包括绑定定制的例子。 我的猜测是,单个字母催促他们下划线,因为它们通常用作表别名? – pieman