-->
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: Annotations - Relationship to non-primary unique key
PostPosted: Wed Aug 29, 2007 10:24 am 
Beginner
Beginner

Joined: Sat May 20, 2006 3:40 pm
Posts: 21
If EntityA has a primary key and another unique key, how can I create a @ManyToOne relationship on EntityB whose foreign key to EntityA will point to the unique key (not the primary key)?

Note that this 'foreign key' doesn't necessarily have to be expressed in the database constraints as a foreign key (though the option might be nice), but it should work as such in the application.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 11:48 am 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
Hi lightbulb,

You should use JoinColumn for that:

ex:
Code:
@Entity
class EntityA {
  long id;
  long uniqueKey;
}


Code:
@Entity
class EntityB {
  @ManyToOne
  @JoinColumn(name="Referenced_Unique_Key", referencedColumnName="uniqueKey")
  EntityA parent;
}


this way you should have a column in EntityB with the name Referenced_Unique_Key that points to EntityA.uniqueKey

I didn't try it, but it should work I think. Anyway, it's a bad practice to have composed keys, there's always a solution to have one key per table. Did you try to have a one to many relationship on EntityA in which you put your second key? (let's say EntityA_Annex with its own id and pointing to EntityA, then have EntityB look to the primaryKey of EntityA_Annex )

A <--(A.id)-- A_Annex <--(A_Annex.id)-- B

Hope it helped...

_________________
Regards,
Zied Hamdi


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 11:53 am 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
Maybe in the other sens too

B --(A_Second.id)--> A_Second <--(A_Second.id)-- A

...

_________________
Regards,
Zied Hamdi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 11:58 am 
Beginner
Beginner

Joined: Sat May 20, 2006 3:40 pm
Posts: 21
I should have mentioned that my collection is actually a @CollectionOfElements, rather than a collection of @Entity. The @JoinColumn with referencedColumnName doesn't seem to work.

Does the @JoinColumn not work because I have an existing generated table? The Hibernate auto-update value is "update", but do I still need to manually modify things? (e.g. if the primary key is an INT but the unique key is a VARCHAR)

The table generated for the collection has two types of columns - ID columns pointing to the primary key (when it should point to the other unique key) of the containing entity, and another set of columns describing the entity in the collection.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 12:29 pm 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
Hi again lightbulb,

I think it's not possible to have a @JoinColumn on a @CollectionOfElements which is a simplification of a table to permit avoiding repetitive code.

You surely must begin by declaring your object as an Entity apart so you can have flexibility on dependencies.

To me, @Embedded or @CollectionOfElements are reserved to very academic cases with no specific constraints.

_________________
Regards,
Zied Hamdi


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.