【问题标题】:Specify UUID generated Id field with Hyperjaxb3使用 Hyperjaxb3 指定 UUID 生成的 Id 字段
【发布时间】:2016-05-20 11:12:40
【问题描述】:

我正在从 xsd 模式生成类。

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

休眠:选择 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>

更新开始 这会在我的 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 和生成的类。

回答 (以答案的形式,而不是模糊的 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 欺骗,但还不确定

【问题讨论】:

  • 这个问题归结为:HJ3生成哪些注解,你想生成什么?
  • @lexicore 谢谢,我已经更新了这个问题。基本上,我不知道应该生成什么 Hibernate/Persistence 注释......我唯一的希望是这个问题很普遍,应该有一个简单的解决方案。
  • 这非常接近为我解决问题。但不完全是。 &lt;ha:GenericGenerator name="uuid" strategy="uuid2"/&gt; 中的ha 的定义是什么,uuid2 是否在某处定义?我经常使用 annox,但从未在如此复杂的注释上使用过,而且我发现的示例都很简单。
  • @RogerParkinson,套件作者似乎对支持他的软件失去了兴趣,所以我也开始使用它,抱歉

标签: hibernate jaxb hyperjaxb


【解决方案1】:

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

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

您已经使用 generator="uuid" 尝试过此操作,并得到类似“生成器 uuid 未知”的信息。这可能是因为您还需要为名称 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 的作者。

【讨论】:

  • 谢谢!我究竟需要如何使用 jaxb2-annotate-plugin?如果我不需要参数,只需要名称和策略(我希望它会起作用)
  • @fedd 我认为README 很能说明问题。或者问另一个问题。简而言之,annox:annotate 带有您的注释(类必须是完全限定的)附加到相应的属性。有关 HJ3 的用法,请参阅 this example
猜你喜欢
  • 2022-01-07
  • 2014-10-29
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 2017-07-29
  • 1970-01-01
  • 2021-06-24
  • 2021-10-16
相关资源
最近更新 更多