Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Composite key as foreign key problem.  RSS feed

nedumangad chandra
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 tables.DEPARTMENT and TEACHER.DepartmentPK(schoolCode,departmentCode) and TeacherPK(shcoolCode,teacherCode) are the Primary Key entities of the corresponding entities Department and Teacher for these tables.I have a departmentCode in Teacher class which is a foreign key to Department class.So as a result,I have a composite foreign key (schoolCode,departmentCode) in my Teacher class and one of these columns(schoolCode) is already a part of primary key of my Teacher class.In this scenario,how can I map a "Many-to-One"relation between these tables.I used the following code in my Teacher class.

Here SCHCOD and DPTCOD are the column names in the tables for schoolCode and departmentCode in the entity classes.For inserting one row in TEACHER table using Teacher entity,I cannot set department since I have used "insertable = false".If I set ïnsertable=true",hibernate will show error since both of the columns should be given as "true".But if I give both of the columns as ïnsertable=true",it will show another error, as multiple insertion for SCHCOD (since schoolCode is a part of primary key in Teacher class).But DPTCOD is not a part of primary key.Please suggest a solution for this...
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!