• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I map composite primary key/foreign key

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

https://coderanch.com/t/218431/ORM/java/Hibernate-Annotations-many-many-association
 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?


 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic