2016-05-20 82 views
1

我从xsd模式生成类。使用Hyperjaxb3指定UUID生成的Id字段

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

休眠:选择NEXTVAL( 'hibernate_sequence') org.hibernate.id.IdentifierGenerationException:com.vsetec.collect.app.generated:该类id必须调用save()之前手动分配。平衡

我的代码是:

<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开始 由此,在我的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字段的另一层问题,因此我会首先尝试将其映射为十六进制字符串,例如。但是,如果你对这个问题有一个可行的解决方案,这是远远不寻常的,这对每个人都有用。

UPD结束

与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生成器附加到一个字符串字段。我无法说出什么是班级定义,因为我使用了“动态地图”。我的任务是从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个字符长

PS。还有与大宗插入一个问题(犯罪嫌疑人UUID愚弄,但尚未确定

+0

这个问题归结为:HJ3生成哪些注释以及您希望生成什么? – lexicore

+0

@lexicore谢谢你,我已经更新了这个问题。基本上,我不知道应该生成何种Hibernate/Persistence注释...我唯一的希望是这个问题很常见,它应该有一个简单的解决方案。 – fedd

回答

0

您就可以使用自定义生成@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" 
     ) 
    } 
) 

这一点,但是,没有一个标准J PA注释,因此HJ3不生成它。您可以使用jaxb2-annotate-plugin来添加此批注。

免责声明:我是Hyperjaxb3jaxb2-annotate-plugin的作者。

+0

谢谢!我到底需要使用jaxb2-annotate-plugin?如果我不需要参数,只是名称和策略(我希望它会起作用) – fedd

+0

@fedd我认为[README](https://github.com/highsource/jaxb2-annotate-plugin)非常具有说明性。或者提出另一个问题。简而言之,带注释的“annox:annotate”(类必须完全合格)附加到相应的属性。有关HJ3的使用,请参阅[本示例](https://github.com/highsource/hyperjaxb3/tree/master/ejb/tests/annox)。 – lexicore