-->
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.  [ 6 posts ] 
Author Message
 Post subject: one-to-many mapping using a foreign key, how?
PostPosted: Wed Mar 16, 2005 10:54 am 
Newbie

Joined: Wed Mar 16, 2005 10:18 am
Posts: 4
Hi all,

we're using Hibernate 2.x and I have a simple problem which I don't know how to solve (even after reading this forum and the specs). Two tables:

[code]

create table list_something (
id int primary key,
translation_id int foreign key references translation(translation_id)
...,
);

create table translation (
id int primary key,
translation_id int not null,
locale_id int not null,
name varchar(100),
...
[/code]

Please note that list_something.translation_id (not the PK!) references translation_id in translation (which isn't a PK either and not unique; translation_id plus locale_id gives an alternate key, but...).

What I'd like to achive is getting a Set of translation objects for the referenced translation_id.

Any ideas how to solve this?

TIA! - Bernd


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 16, 2005 12:27 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 10:26 am
Posts: 22
check this out
http://www.hibernate.org/hib_docs/v3/re ... n-onetoone


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 5:30 am 
Newbie

Joined: Fri May 13, 2005 2:46 am
Posts: 2
Location: Gruitrode, Belgium
theBernd, did you manage to find a solution for your translation problem? I'm interested to read it...

Thanks.


Top
 Profile  
 
 Post subject: nope, sorry
PostPosted: Tue Jun 28, 2005 5:34 am 
Newbie

Joined: Wed Mar 16, 2005 10:18 am
Posts: 4
Hi, no, I didn't understand the suggestion above and since we have been under extreme time pressure we made a datamodel change (hack would be more appropriate).

I read the release notes to Hibernate 3.x (comparision to 2.x, new features, whatever) and it sounds to me like this is a new 3.x feature.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 7:09 am 
Regular
Regular

Joined: Thu Apr 29, 2004 5:08 pm
Posts: 56
Location: Montreal, Quebec, Canada
Hi theBernd,

First of all,

Quote:
references translation_id in translation which isn't a PK either and not unique


Well, if translation_id in translation isn`t unique, you shouldn't using a set. A set doesn't allows duplicate elements.

Second,

Quote:
translation_id plus locale_id gives an alternate key, but...)


I would like to know what is the end of your sentence here, but here is what I would suggest. Use a composite-key for your set and override .equals() and .hashcode in the translation object to reflect the translation table natural key (translation_id + locale_id) :

Code:
public class Translation implement Serializable
{
    private Serializable id; // i prefer to use Serializable for ids because this let me change its implementation later without altering this code
    private Serializable translationId;
    private Serializable localeId; // you should consider using a java.util.Locale object...

    [...]

    public boolean equals( Object o )
    {
        if( this == o ) return true;
        if( !(o instanceof Translation) ) return false;

        Translation other = (Translation)o;

        return   getTranslationId().equals( other.getTranslationId() )
                    && getLocaleId().equals( other.getLocaleId() );
    }

    public int hashCode()
    {
        int result;

        result = getTranslationId();
        result = 29 * result + getLocaleId();

        return result;
    }
}


Then, map your set in the list_something object this way:

Code:
<set name="translations" table="translation"> <!-- use cascade, inverse, etc as necessary-->

    <key>
        <column name="translation_id"/>
   <column name="locale_id"/>
    </key>

    <one-to-many
        class="Translation"/>
   
</set>


Let me know about it,

_________________
- Frank


Top
 Profile  
 
 Post subject: Thanks for the info
PostPosted: Tue Jun 28, 2005 8:13 am 
Newbie

Joined: Wed Mar 16, 2005 10:18 am
Posts: 4
Hi Frank,

thanks for the info.

The related work has been my first Hibernate project and at the same time I had to find my way through another framework I haven't work with before. Since even co-workers with quite some Hibernate experience didn't know how to solve this, we changed the datamodel to make things work.

I really appreciate your help and will look back at it ASAP (month end means a two projects deadline). I can't remember what I meant with the ",but...". I think it was something along the lines that hibernate just cares about / references the primary key and not alternate keys, especially not composite ones (which may be wrong).

Anyway, thanks a lot. - Bernd


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