【发布时间】: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;
文档在这里:
它描述了应该如何注释 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 注释......我唯一的希望是这个问题很普遍,应该有一个简单的解决方案。
-
这非常接近为我解决问题。但不完全是。
<ha:GenericGenerator name="uuid" strategy="uuid2"/>中的ha的定义是什么,uuid2是否在某处定义?我经常使用 annox,但从未在如此复杂的注释上使用过,而且我发现的示例都很简单。 -
@RogerParkinson,套件作者似乎对支持他的软件失去了兴趣,所以我也开始使用它,抱歉