wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate foreign key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate foreign key " Watch "Hibernate foreign key " New topic
Author

Hibernate foreign key

Cheenath Ajay
Ranch Hand

Joined: Jan 26, 2006
Posts: 32
Dear All,

I am not strong in Database concepts,

I am having two tables,
User and Salary

In User table UserID is my primary key and in Salary table UserID and Salary_Date are composite keys. How can i set UserId as foreign key in Salary table? Can i use cascade on delete through Salry mapping file?

Please help me out if it is possible or please let me know where i did wrong?

Thanks in Advance,

Cheenath.
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Originally posted by Cheenath Ajay:
Dear All,

I am not strong in Database concepts,

I am having two tables,
User and Salary

In User table UserID is my primary key and in Salary table UserID and Salary_Date are composite keys. How can i set UserId as foreign key in Salary table? Can i use cascade on delete through Salry mapping file?

Please help me out if it is possible or please let me know where i did wrong?

Thanks in Advance,

Cheenath.


My preference would be for Salary to have a non-composite key. Don't include SALARY_DATE as part of the key.

USER_ID can be foreign key in SALARY:



%
Cheenath Ajay
Ranch Hand

Joined: Jan 26, 2006
Posts: 32
Thanks for the reply!!

but my situation is like sal_datae and userID should be composite key and i cant avoid that by making it as primary key, is there any way like that?

Hope you can help me to find a solution.

Thanks again

Cheenath
Cheenath Ajay
Ranch Hand

Joined: Jan 26, 2006
Posts: 32
Leave the matter of Salary table, just imagine Group table where a User will be having only one group and a Group can have more than one User

USERID+GroupID so it should be a composite key in Group table, am i righ


Please help me out!!

Cheenath
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Forget composite keys altogether, use surrogate keys (like oids) instead of natural keys (like actual meaningful data columns), and use a combination of not null and unique constraints to get the data integrity right. Then, you will have no problem with referential stuff, because you simply reference a surrogate key from one table in another.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate foreign key
 
Similar Threads
Mapping issue
Hypersonic DB question
Spring + Hibernate
Beginner Problem: son & parent
outer join in HQL