-->
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.  [ 5 posts ] 
Author Message
 Post subject: composite id and generator
PostPosted: Fri Apr 13, 2007 3:50 pm 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Hibernate version: 3
Oracle 9i

Is possible to use composite id with generator class, like this?

Code:
<hibernate-mapping>
    <class name="ModuleTO" table="t_module">
       <composite-id>
           <key-many-to-one name="product" class="ProductTO">
              <column name="product_id"/>
           </key-many-to-one>
           <key-property name="id" column="module_id">
         <generator class="native">
            <param name="sequence">seq_module</param>
         </generator>
                </key-property>
   </composite-id>
    </class>
</hibernate-mapping


thank you!!!!

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 3:20 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
No, it's not. Check the hibernate dtd, you'll see it's not legal to have a <generator> inside <composite-id> tag...

Simply because composite-id are meant to be business keys, not technical one. So they can't be automatically generated.

Are you using a legacy db or is it a new one? Because composite ids are not best practices, so trying and change it would be better (see reference doc about surrogate keys and so on).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 19, 2007 8:14 am 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
thank you very much!

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 19, 2007 8:29 am 
Beginner
Beginner

Joined: Wed Jun 14, 2006 9:44 am
Posts: 22
You can to do a method to returns the last row inserted.


Top
 Profile  
 
 Post subject: If composite keys cannot have generated fields...
PostPosted: Thu Jul 26, 2007 12:15 am 
Newbie

Joined: Wed Jul 25, 2007 11:41 pm
Posts: 1
I am new to J2EE and felt certain that my failure to know how do this had to do with that fact. But, I see plenty of statements like:

Quote:
Simply because composite-id are meant to be business keys, not technical one. So they can't be automatically generated.

and
Quote:
We regard this as an extremely strange thing to want to do. If you have a generated surrogate key, why not just make it be the primary key?


If composite keys cannot have generated fields then I need some DB advise.

I have a database for which data is going to be asynchronously replicated between several sites. Several of the tables to be replicated have generated keys. The key must be generate by the local database, but the primary key must be globally unique.

My normal solution to this problem is primary key (location_id, generated_id). But this is not supported and seems to be regarded as "extremely strange".

A second solution is to start the generated_id at a value that is determined by the location. On the practical front I think this is a pain because I would have to make certain that each location is assigned a range and should I trip I will have a mess. On an esthetic front I feel like doing this to id field makes it non atomic. (i.e. it is like location_id * 10^n + generated_id).

A third option is to have a local non replicated table that fires a trigger to populate a replicated table. The local table would not need the location id, but the global table could still be contain a globally unique primary key. Of coarse, this gives me two copies of the data.

So, what am I missing? What is the "right" way to do this?

BTW...The DB I was mandated to use does not have sequences. Which, sucks because I love sequences.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.