1

How to use a composite key as foreign key in table using JPA.

Say, I have a table u with a,b as composite key. There are other fields though. There is another table, say V, in which a will be a foreign key.

Aira
  • 11
  • 2
  • V should have two columns to reference U.a and U.b. A single column can't be a foreign key to two columns of another table. – JB Nizet Sep 20 '17 at 07:00
  • Check [here](https://stackoverflow.com/questions/3323751/jpa-hibernate-composite-foreign-key-mapping?rq=1). It might help – Procrastinator Sep 20 '17 at 07:03
  • if You are using Hibernate then refer this http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite – ThinkTank Sep 20 '17 at 07:19

1 Answers1

0

Use an @IdClass or an @EmbeddedId.

Check out http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0 and Which annotation should I use: @IdClass or @EmbeddedId.