File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I map composite primary key/foreign key

 
Jay Abrahm
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Abrahm
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic