简体   繁体   中英

Specify UUID generated Id field with Hyperjaxb3

I am generating classes out of xsd schemas.

I can't figure out how to tell that an object identifier should be a UUID generated in program. My error is:

Hibernate: select nextval ('hibernate_sequence') org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.vsetec.collect.app.generated.Balance

My code is:

<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 This generates the following in my 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;
}

If I add

<orm:generated-value generator="uuid"/>

, it says something like "there is no generator by the name uuid"

If I add

<hj:generator generatorClass="uuid"/>

, nothing is really added, no annotation to add a UUID generator or anything. I did a search for "uuid" substring so I know. The above mentioned error remains.

I want to make Hibernate to generate an identfier as a UUID. The doc says it is achieved with annotations like the following:

@Id
@GeneratedValue
public UUID id;

The documentation is here:

http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-uuid

It describes how a field of UUID type should be annotated. I guess it is another layer of questions on how to map UUID fields in Jaxb, so I would first try to map it as a hex String, for example. But if you have a working solution for this problem which is far from uncommon, it would be useful for everybody.

upd end

Previously with HBM I did this like the following:

<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

I have no idea what annotations this xml mapping corresponds to, but it worked. It seems that it attaches a UUID generator to a String field. I can't say what was the class definition because I used "dynamic-maps". My task is just to switch from HBM and dynamic maps to Hyperjaxb and generated classes.

ANSWER (in a form of an answer rather that a vague rtfm style hint)

    <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 should be of 36 characters long

ps. there is still a problem with mass inserts (suspect uuid dupes, but yet not sure

You can generate @GeneratedValue with a customization like:

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

You've tried this with generator="uuid" and got something like "generator uuid is not known". This is probably because you also need to actually configure a generator for the name uuid , like in Hibernate docs:

@GenericGenerator(
    name = "uuid",
    strategy = "org.hibernate.id.UUIDGenerator",
    parameters = {
        @Parameter(
            name = "uuid_gen_strategy_class",
            value = "org.hibernate.id.uuid.CustomVersionOneStrategy"
        )
    }
)

This is, however, not a standard JPA annotation so HJ3 does not generate it. You can use the jaxb2-annotate-plugin to add this annotation.

Disclaimer: I'm the author of Hyperjaxb3 and jaxb2-annotate-plugin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM