-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: UID generation for composite key
PostPosted: Mon Aug 09, 2004 10:32 am 
Newbie

Joined: Tue Aug 03, 2004 11:34 am
Posts: 8
I, i'm using hibernate version 2.1.4,
I am currently migrating to hibernate.
I'm very intersted in using UID generation feature of hibernate for generating a part of a primary key.(
Code:
<generator>
mapping element).

But the mapping where i need such a feature contains a composite key.
Here is the mapping:

Code:
<hibernate-mapping>
    <class
        name="com.atronicsystems.database.data.MessageHistory"
        table="SHISMSG"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <composite-id
            name="id"
            class="com.atronicsystems.database.data.MessageHistoryId"
        >
                     <key-property
                        name="casinoId"
                        type="java.lang.Integer"
                        column="ID_CASINO"
                        length="10"
                />

                     <key-property
                        name="messageId"
                        type="java.lang.String"
                        column="ID_MSG"
                        length="21"
                />

        </composite-id>


....

I'cant change the primary key, it has to be a composite key!
But i had for each new record to generate a unique id for the
Code:
messageId
property (
Code:
casinoId
property is known).
So if i can't use the generate element in my composite key for the message id attribute, is there a way to use one of the generator algorithm outside of the mapping.
In order for me to set the
Code:
messageId
attribute with a unique id generated by hibernate. (I don't want to rewrite a generator algorithm code :-( ).

Does anyone have a solution?

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2004 11:34 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
If you are generating a UUID, why not just use THAT as your PK rather than having it as a composite?

You can still enforce a unique constraint on the DB side if it makes business sense.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2004 12:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Id generators are not supported for composite ids. Composite ids must be assigned.

As part of assigning that id, sure you can manually instantiate and use one of the id generators.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2004 3:31 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
If you're using the DAO pattern, you can provide a create() Method on the DAO, which does the id assignment. This way you don't have to do it in the business logic.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 3:47 am 
Newbie

Joined: Tue Aug 03, 2004 11:34 am
Posts: 8
VampBoy wrote:
If you are generating a UUID, why not just use THAT as your PK rather than having it as a composite?

You can still enforce a unique constraint on the DB side if it makes business sense.


If i'm not just using the UUID as my PK it's because i'm not authorized to change database architecture (i have to use it like it is).

But is it possible to keep the orginal database architecture (with a composite PK) and to define in my hibernate mapping the UUID (messageId) as my pk and a single constraint on the casinoId.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 3:53 am 
Newbie

Joined: Tue Aug 03, 2004 11:34 am
Posts: 8
steve wrote:
Id generators are not supported for composite ids. Composite ids must be assigned.

As part of assigning that id, sure you can manually instantiate and use one of the id generators.


That's exactly what i want: manually insantiate and use one of the id generators.

But the point is: how to do this?
Have you some examples?

I see in API documentation that there is a IdentifierGeneratorFactory, but it's use looks to me mysterious.

Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 3:58 am 
Newbie

Joined: Tue Aug 03, 2004 11:34 am
Posts: 8
ernst_pluess wrote:
If you're using the DAO pattern, you can provide a create() Method on the DAO, which does the id assignment. This way you don't have to do it in the business logic.

HTH
Ernst


I'm using a DAO, but have you an example about how to do it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 2:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Why use the factory? You just said you use a known id generator. Just instantiate the class representing that generator, and call its generate() method:
Code:
    // UUID generators support a "seperator" property, but its optional...
    Properties props = new Properties();
    props.put("seperator", "-");

    IdGenerator generator = new UUIDHexGenerator();
    generator.configure(props);

    ...

    Object uuid = generator.generate( (SessionImplementor) session, entity );


Note, the UUID generators do not use either of the parameters passed to the generate method. So this is also valid for them:
Code:
    Object uid = generator.generate(null, null);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.