簡體   English   中英

使用Hyperjaxb3指定UUID生成的Id字段

[英]Specify UUID generated Id field with Hyperjaxb3

我正在使用xsd架構生成類。

我無法弄清楚如何判斷對象標識符應該是程序中生成的UUID。 我的錯誤是:

Hibernate:select nextval('hibernate_sequence')org.hibernate.id.IdentifierGenerationException:在調用save()之前必須手動分配此類的ID:com.vsetec.collect.app.generated.Balance

我的代碼是:

<xsd:complexType name="balance">
    <xsd:annotation>
        <xsd:documentation>Balance Amounts</xsd:documentation>
    </xsd:annotation>

    <xsd:all>
        <xsd:element name="comment" type="longNameString"/>
    </xsd:all>        

    <xsd:attribute name="typeCd" type="referenceCode"/>
    <xsd:attribute name="amount" type="xsd:decimal"/>
    <xsd:attribute name="currencyCd" type="referenceCode"/>
    <xsd:attribute name="dateLoad" type="xsd:date"/>
    <xsd:attribute name="historizedOn" type="historizedDate"/>
    <xsd:attribute name="id" type="uuidString" minOccurs="0">
        <xsd:annotation>
            <xsd:appinfo>
                <jaxb:property>     
                    <jaxb:javadoc>@hyperjaxb.hibernate.id unsaved-value="null" generator-class="uuid.hex"</jaxb:javadoc>
                </jaxb:property>                    
                <hj:id>
                    <!--<hj:generator generatorClass="uuid"/>-->
                    <orm:column name="id"/>
                    <!--<orm:generated-value generator="uuid"/>-->
                </hj:id> 
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>        
</xsd:complexType>

upd start這在我的java中生成以下內容:

/**
 * @hyperjaxb.hibernate.id unsaved-value="null" generator-class="uuid.hex"
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
@Id
@Column(name = "id", length = 32)
public String getId() {
    return id;
}

如果我加

<orm:generated-value generator="uuid"/>

,它說“沒有名稱為uuid的發電機”

如果我加

<hj:generator generatorClass="uuid"/>

沒有添加任何東西,沒有添加UUID生成器或任何東西的注釋。 我搜索了“uuid”子串,所以我知道。 上述錯誤仍然存​​在。

我想讓Hibernate生成一個作為UUID的identfier。 該文檔說它是通過以下注釋實現的:

@Id
@GeneratedValue
public UUID id;

文檔在這里:

http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-uuid

它描述了如何注釋UUID類型的字段。 我想這是關於如何在Jaxb中映射UUID字段的另一層問題,所以我首先嘗試將其映射為十六進制字符串,例如。 但是如果你有一個解決這個問題的工作解決方案並不常見,那對每個人都有用。

更新結束

以前使用HBM我做了以下這樣的事情:

<class entity-name="Balance">
    <cache usage="read-write"/>
    <comment>
        Balance Amounts
    </comment>
    <id name="id" type="string" length="32">
        <generator class="uuid"/>
    </id>       
    <property name="currencyCd" type="string" length="32"/>
    <property name="amount" type="big_decimal"/>
    <property name="comment" type="string" length="255"/>

    <property name="historizedOn" type="date"/>
</class>

UPD

我不知道這個xml映射對應的注釋,但它有效。 它似乎將UUID生成器附加到String字段。 我不能說什么是類定義因為我使用了“動態地圖”。 我的任務是從HBM和動態映射切換到Hyperjaxb並生成類。

答案 (以答案的形式而不是模糊的rtfm樣式提示)

    <xsd:attribute name="id" type="uuidString" minOccurs="0">
        <xsd:annotation>
            <xsd:appinfo>
                <hj:id>                        
                    <orm:column name="id"/>
                    <orm:generated-value generator="uuid"/>
                </hj:id> 
                <annox:annotate>
                    <ha:GenericGenerator name="uuid" strategy="uuid2"/>
                </annox:annotate>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>        

uuidString的長度應為36個字符

PS。 質量插入仍然存在問題(可疑的uuid欺騙,但還不確定

您可以使用以下自定義生成@GeneratedValue

<hj:id>
    <orm:generated-value strategy="..." generator="..."/>
</hj:id>

你用generator="uuid"嘗試了這個,並得到類似“發電機uuid未知”的東西。 這可能是因為您還需要為名稱uuid配置生成器,就像在Hibernate docs中一樣:

@GenericGenerator(
    name = "uuid",
    strategy = "org.hibernate.id.UUIDGenerator",
    parameters = {
        @Parameter(
            name = "uuid_gen_strategy_class",
            value = "org.hibernate.id.uuid.CustomVersionOneStrategy"
        )
    }
)

但是,這不是標准的JPA注釋,因此HJ3不會生成它。 您可以使用jaxb2-annotate-plugin添加此批注。

免責聲明:我是Hyperjaxb3jaxb2-annotate-plugin的作者

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM