Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate foreign key

 
Cheenath Ajay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic