JPA JoinColumns Annotation Example

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We have seen how JoinColumn annotation can be used in Many to One relationship when the entity primary key is composed of a single column. There are cases when composite keys can be used for entities. This JPA JoinColumns Annotation Example demonstrates how we can map composite primary keys to entities and use them in relationships.

 JPA JoinColumns Annotation Example:

Consider following scenario

  • Employee entity with fields employeeId as EmbeddedId, firstName, lastNamesalary and phones.
  • EmployeeId is an embeddable class which is used for employeeId of Employee.
  • Phone entity with fields idPhone, phoneNumber, provider and owner.

We want to model Phone to Employee relationship as ManyToOne but the tricky part is that Employee has a composite primary key. JPA JoinColumns annotation example demonstrates the mapping.

  • @JoinColumns annotation takes an array of @JoinColumn annotations.
  • These @JoinColumn annotations map database table columns.

Now according to above model, our Phone table in database should have two columns to refer to primary key of Employee. Database diagram becomes as below.

JPA JoinColumns Annotation Example

Entities for JPA JoinColumns Annotation Example:

Employee Entity:

 Phone Entity:

Point to notice in this JPA JoinColumns Annotation Example:

  • Value of @JoinColumns is an array consisting @JoinColumn elements.
  • name subelement defines the name of column used in Phone table by specifying  name = "EMP_ID" .
  • referencedColumnName subelement defines the name of primary key column in Employee table being referenced by column specified using name element i.e.  referencedColumnName = "IDEMPLOYEE" .
  • Simillary another @JoinColumn is used for second primary key column.

Now we can test JPA JoinColumns Annotation Example using code below.

After persisting tables are populated as follows,

Employee Table:

Prasad Kharkar 0 Pune 2

Phone Table:

9 6456456456 BSNL Pune 2
10 45345345 Idea Pune 2

You can find the enity by code below jus to be sure you are getting desired results.

I hope this article helped understand JPA JoinColumns Annotation Example.

Please feel free to provide feedback. Happy learning 🙂


Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

5 thoughts on “JPA JoinColumns Annotation Example

  • November 18, 2016 at 10:16 pm

    Interesting article.
    In my case one of the entity has 3 extra fields differents

    Relationship “1 to Many” from TableA to Table B

    ID (PK)
    COD (PK)
    YEAR (PK)
    MONTH (PK)

    ID (PK)
    DAY (PK)

    As you see, TableA and TableB are related just by id field. I get this message:
    referencedColumnNames(id) of TableB.TableA referencing TableA not mapped to a single property

    In Table A
    private Set TABLEB;

    In Table B
    @JoinColumn(name=”id”, referencedColumnName=”id”)
    //bi-directional many-to-one association to Table A
    private TableA tableA;

  • Pingback:JPA. Relationships — Код истины

  • January 18, 2018 at 12:31 pm

    When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.

    I am getting the baove error.. Please help


Leave a Reply

Your email address will not be published. Required fields are marked *