2

我正在尝试使用 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) 正在破坏我的代码,因为休眠抱怨无效的列映射。

到目前为止我尝试过的对问题没有影响:- 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>

我在这里想念什么,请任何人!

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

选择 demo0_.PLAYERID 作为 PLAYERID1_0_,demo0_.G_ 作为 G_2_0_ 从 DEMO demo0_ where 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而不是由 hyperjaxb 生成的G_ ,将其更改为 G(手动)解决了这个问题

4

1 回答 1

0

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

例如,我们正在使用 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>
于 2016-08-05T07:17:14.940 回答