1

我正在从 xsd 模式中生成类。

我不知道如何判断对象标识符应该是程序中生成的 UUID。我的错误是:

Hibernate: select nextval ('hibernate_sequence') org.hibernate.id.IdentifierGenerationException: 这个类的 ids 必须在调用 save() 之前手动分配: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。该文档说它是通过如下注释实现的:

@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>

更新

我不知道这个 xml 映射对应于什么注释,但它有效。似乎它将 UUID 生成器附加到 String 字段。我不能说类定义是什么,因为我使用了“动态映射”。我的任务只是从 HBM 和动态映射切换到 Hyperjaxb 和生成的类。

ANSWER (以答案的形式,而不是模糊的 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 个字符

附言。批量插入仍然存在问题(怀疑 uuid 欺骗,但还不确定

4

1 回答 1

0

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

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

您已经尝试过这个generator="uuid"并得到了类似“generator uuid is not known”的东西。这可能是因为您还需要为 name 实际配置一个生成器uuid,就像在 Hibernate 文档中一样:

@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的作者。

于 2016-05-22T22:53:18.173 回答