-->
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.  [ 10 posts ] 
Author Message
 Post subject: How can I map two keys without composite-id ??
PostPosted: Wed Mar 07, 2007 2:59 am 
Newbie

Joined: Sat Mar 03, 2007 3:14 am
Posts: 16
I have this too tables:

CREATE TABLE PersonalData(
ID_PersonalData VARCHAR2(20) NOT NULL,
ID_Person VARCHAR2(20) NOT NULL,
other VARCHAR2(20)
CONSTRAINT PK46 PRIMARY KEY (ID_PersonalData,ID_Person)
);

CREATE TABLE Person
ID_Person VARCHAR2(20) NOT NULL,
otherfield VARCHAR2(20)
CONSTRAINT PK31 PRIMARY KEY (ID_Person)
);
And i need ID_PersonaData like a generated identifier ..
How can i do that ? ..
I never map two ids in the same table without composite-id, and in this case is not posible ... !!
Please help ..


Top
 Profile  
 
 Post subject: Re: How can I map two keys without composite-id ??
PostPosted: Wed Mar 07, 2007 3:20 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
smlghst wrote:
I never map two ids in the same table without composite-id, and in this case is not posible ... !!

Well, maybe you could precise your needs.

It's not possible to map a table that has more than one primary key without using composite id. But there are two ways for doing this: using a special composite class to store the ids (recommended method), or directly storing the props without any new class.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 4:32 am 
Newbie

Joined: Wed Feb 07, 2007 7:42 am
Posts: 13
Location: Spain
You have got two posible solutions:

1.- Use a surrogate keys in yor PersonalData table. Composite keys make more difficult the maintenance. Then. you can creata an unique constraint in your database with ID_PersonalData and ID_Person columns (this combination of columns are a candidate key).


2.- I suppose than the column ID_Person of your PersonalData table is a foreign key from Person Table. If one instance of PersonalData hasn't got its own lifecycle, you can understaand the class PersonalData how a type value from Person class. You must mapped the class Person how:

Code:
<class table="Person>
......

<composite-element class="PersonalData">
           ...............
        <property name="other" column="other" not-null="true"/>
</composite-element>


......
</class



Top
 Profile  
 
 Post subject: RE: seems like is other problem !!
PostPosted: Wed Mar 07, 2007 5:17 am 
Newbie

Joined: Sat Mar 03, 2007 3:14 am
Posts: 16
ok .. but i need that id_PersonData by generated .. how can i do that ?
Thanks for your replies ..


Top
 Profile  
 
 Post subject: Let me explain better !!
PostPosted: Wed Mar 07, 2007 5:26 am 
Newbie

Joined: Sat Mar 03, 2007 3:14 am
Posts: 16
This two tables can not be change, the model is already deployed and the person_id is a foreign key of the table PersonalData but also a key so .. I need to generate the ID_PersonalData .. and the idea of a UserType Composite-Id that use a generator to do that make sense ..
dont you think ??
What others option do i have ??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 6:33 am 
Newbie

Joined: Wed Feb 07, 2007 7:42 am
Posts: 13
Location: Spain
If you need that ID_Personal_data would be a generated identifier, then, the primary key of your PersonalData table must be an unique column, ID_PersonalData. Think in it. This column will be unique and not-null, you can use it how primary key.

Then, you can make an unique constraint in your database with the columns ID_PersonalData and ID_Person. (This constarints it hasn't got munh sense, because one column is unique yet).


Top
 Profile  
 
 Post subject: Thanks .. !!
PostPosted: Wed Mar 07, 2007 7:21 am 
Newbie

Joined: Sat Mar 03, 2007 3:14 am
Posts: 16
Yes .. but what about the composite-id .. how i make it to one of the Keys been generated .. in this case PersonalData Id .. ??
I was reading in the night, and seems like i have to implement a CompositeUserType and IdentifierGenerator for that .. ?
is that true ??
There no something already for this http://hibernate.bluemars.net/117.html?cmd=prntdoc


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 10:32 am 
Newbie

Joined: Wed Feb 07, 2007 7:42 am
Posts: 13
Location: Spain
If you need than one key (ID_Personaldata) would be generated, you don't need a composite key, because ID_Personaldata is yet a primary key (is unique and is not-null).

Which is the reason by you have a composite-id?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 11:15 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Imo, if your tables really cannot change: you have only one solution. Map the columns as a composite-id as it has to (since it is a composite primary key) and manage the sequence by yourself to set one of those columns.

Write some wrapping code that will retrieve the next sequence number you need, then just set it manually.

I'm not sure there's another way for you at the moment.

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


Top
 Profile  
 
 Post subject: Thanks .. !!
PostPosted: Wed Mar 07, 2007 10:30 pm 
Newbie

Joined: Sat Mar 03, 2007 3:14 am
Posts: 16
i´m in that situation .. i can´t change the design of the dataBase .. so .. i hear that you can do that with COmpositeUserType and identifierGenerators .. but. there is no example or other reading that help to implement that . !!


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