2

hyperjaxb を使用して JPA アノテーション付き Java クラスを生成しようとしていますが、問題が 1 つあります。どんな提案も歓迎します:-

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

ここで何が欠けていますか、誰かお願いします!

更新: 以下のHibernateクエリ: - Hibernate:

DEMO demo0_ から PLAYERID1_0_ として demo0_.PLAYERID、G_2_0_ として demo0_.G_ を選択します。

痕跡:

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

私の経験から、Hyperjaxb は、その列の名前が SQL ダイアレクトの予約語である場合、列の名前 (テーブルの疑いがあるのでしょうか?) の末尾にアンダースコアを追加します。

たとえば、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 に答える