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.  [ 12 posts ] 
Author Message
 Post subject: how to map table relation not using foreign key?
PostPosted: Wed Apr 13, 2005 5:57 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Hi

I have 2 tables - tab1 and tab2. tab1.ref1 is unique NOT FOREIGN KEY referencing to tab2.pk2 which is CAN'T BE A KEY because tab2.pk2 is only a part of composite primary key for tab2.

Code:
CREATE TABLE tab1 (
    pk1 INTEGER NOT NULL PRIMARY KEY,
    ref1 INTEGER NOT NULL UNIQUE,
    ...
);

CREATE TABLE tab2 (
    pk1 INTEGER NOT NULL (REFERENCES tab3 <- maybe irrelevant for this problem),
    pk2 INTEGER NOT NULL,
    ....,
    PRIMARY KEY (pk1, pk2)
);


I'd like to have:
Code:
class Tab1 {
    Ineger id;
    Tab2 tab2;
    ....
}

Please tell me something how to map this. Please, I beg you!
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 6:26 am 
Beginner
Beginner

Joined: Thu Oct 16, 2003 7:30 am
Posts: 21
Location: Brussels
You could try to:

1. Create a fake property mapping to tab2.pk2, with the insert and update attributes of the <property/> set to false.

2. Use the property-ref attribute of the <many-to-one/> element for the tab2 field of the Tab1 class.

Johan.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 7:32 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
If tab2.pk2 is part of a composite key, it seems that your class model is wrong, since there may be more than one tab2 record per tab1 record.

I.e.
Code:
class Tab1 {
    Ineger id;
    List<Tab2> tab2s;
    ....
}


Either way, please read the reference documentation on how to map this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 7:48 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Hi jandries

Thanks for your reply, you are the first while I've posted my problem three times here in different formulations with no response. Unfortunately I haven't understood you much. Please be so kind (you or anyone else) to complete my hibernating mapping:

Code:
<class name="Tab1" table="TAB1">
<id name="id" column="pk1">
  <generator class="native" />
</id>

?????
Please add the mapping to Tab2:
??? <many-to-one unique="true" name="tab2" column="ref1" class="Tab2" ...> ... doesn't work???

PS: I have two such properties in Tab1 - both are unique
?????

</class>

<class name="Tab2" table="TAB2">
<composite-id>
  <key-many-to-one name="pk1" column="pk1" class="TAB3" />
  <key-property name="pk2" column="pk2" />
</composite-id>

...
</class>

THANKS IN ADVANCE!
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 7:55 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
hi mate

This mapping should be unique therefore there is no need of collection. My problem is described in specific situation at http://forum.hibernate.org/viewtopic.php?t=940821. I can't do better DB design for that. If you can, please tell me how.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 8:19 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
Personally, I would create different tables for title and description, with book_id + language_id as primary key in both (if not using synthetic keys).

Otherwise: what would be the value of your title/description Maps (with language as key)? Just a String or an object? There are no columns except the key columns in the schema at the link you provided...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 8:24 am 
Beginner
Beginner

Joined: Thu Oct 16, 2003 7:30 am
Posts: 21
Location: Brussels
Try something like

Code:
<class name="Tab1" table="TAB1">
<id name="id" column="pk1">
  <generator class="native" />
</id>

<many-to-one name="tab2" class="Tab2" column="ref1" property-ref="fake"/>

</class>

<class name="Tab2" table="TAB2">
<composite-id>
  <key-many-to-one name="pk1" column="pk1" class="TAB3" />
  <key-property name="pk2" column="pk2" />
</composite-id>

<property name="fake" column="pk2" insert="false" update="false"/>

</class>


Sections 5.1.9 and 5.1.10 give you more details.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 8:39 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
mate,

Thanks for reply. Yes, they should be Maps where language would be a key. Creating separate table would be clear, but I use translated messages on more places (three for now ;-) ) and creating new table for each such field would be expensive. Any better idea?

jandries

Thanks. But I'd be expecting <one-to-many> (or many-to-many ???) mapping, because I need Map. Sorry I didn't mention it on the begining very clearly :-/ ... see my discussion with mate and that link.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 8:50 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
zdila wrote:
Creating separate table would be clear, but I use translated messages on more places (three for now ;-) ) and creating new table for each such field would be expensive.

In what way expensive? We have 50+ tables for texts in different languages.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 9:27 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
wow ... so there is no better way to implement this? If not, I'll accept it.

Just to be sure:
If I have eg. 3 tables and 1st table has 2 fields to be i18ned, 2nd has 3 such fields and 3rd has 1 such field then I must create 2 + 3 + 1 = 6 many-to-many relations (like table LineItem in chapter 23.3)?

Thanks
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 9:40 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
zdila wrote:
so there is no better way to implement this?

"Better" as in...? Fewer tables? Better performance? Clearer data model?

zdila wrote:
Just to be sure:
If I have eg. 3 tables and 1st table has 2 fields to be i18ned, 2nd has 3 such fields and 3rd has 1 such field then I must create 2 + 3 + 1 = 6 many-to-many relations (like table LineItem in chapter 23.3)?

That's probably what I would do.

The only alternative I see is to have a discriminator column in the message table that tells you what kind of message it is. MySQL example:
Code:
CREATE TABLE (
   book_id INT,
   type ENUM('TITLE','DESCRIPTION','FOO','BAR'),
   language_id INT,
   text TEXT,
   PRIMARY KEY(book_id, type, language_id)
)

And then you would use the where-attribute (such as where="type = 'TITLE'") in your mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 9:49 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Thanks mate for your help and your afford. I'll try to do it that way. So I am little bit more clever again :-)


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