| Author |
How do I map composite primary key/foreign key
|
Jay Abrahm
Ranch Hand
Joined: May 28, 2008
Posts: 182
|
|
I have the following table structure
How do I map the composite primary key of DepartmentMonths to YearDepartment and Manager using annotations ?
Do I have to use @Embeddable annotation or is there another way to do this.
|
 |
Jay Abrahm
Ranch Hand
Joined: May 28, 2008
Posts: 182
|
|
It is working now but the primary key should have been a composite key of managerId & yearDepartmentId
I had to battle with it a bit but adding updatable = false, insertable = false fixed it.
@Id
@GeneratedValue
@Column(name="Manager_Id")
private int managerId;
@ManyToOne
@JoinColumns( { @JoinColumn(name = "Manager_Id", referencedColumnName = "Manager_Id", updatable = false, insertable = false) })
private Manager manager;
@Column(name="Year_Department_Id")
private int yearDepartmentId;
@ManyToOne
@JoinColumns( { @JoinColumn(name = "Year_Department_Id", referencedColumnName = "Year_Department_Id", updatable = false, insertable = false) })
private YearDepartmentId yearDepartmentId;
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
You'll need an annotation or something on yearDepartentId.
You can always use a compound key class. Here's an example where I have a copound key of userId and bankId
Then in your code, you declare the @Id of this type:
How To Use Compound Primary Keys In Your Code
Other options include using the @IdClass annotation or @EmbeddedId. This tutorial covers each of them. But that other field needs to be treated somehow.
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
Jay Abrahm
Ranch Hand
Joined: May 28, 2008
Posts: 182
|
|
Thanks, the tutorial was very helpful. I was able to get the composite key setup.
I have a couple of questions though.
1. The tutorial does not show how list can be retrieved for a composite key instead of
2. I have noticed @Id being used for the getter instead of the variable in many examples. Is there any advantage in doing this.
|
 |
Jay Abrahm
Ranch Hand
Joined: May 28, 2008
Posts: 182
|
|
Forget it, I figured out how to retrieve the hibernate objects from the composite key.
I had a question on how to display data which is a combination of YearDepartment and DepartmentMonths. DepartmentMonths is like a join table between YearDepartment and Manager which has additional data.
How do get data from a join table which has other attributes ?
The following post talks about it but I could see no solution
http://www.coderanch.com/t/218431/ORM/java/Hibernate-Annotations-many-many-association
|
 |
Jay Abrahm
Ranch Hand
Joined: May 28, 2008
Posts: 182
|
|
I was able to get the mappings done but I still need help with the query. I got help from the following URL Mapping Association tables with attributes
I get the following exception
org.hibernate.QueryException: illegal attempt to dereference collection
How do I get just one record from the collection ?
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: How do I map composite primary key/foreign key
|
|
|