繁体   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