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:

FIRSTNAME LASTNAME SALARY BRANCHNAME IDEMPLOYEE
Prasad Kharkar 0 Pune 2

Phone Table:

IDPHONE PHONENUMBER PROVIDER LOCATION EMP_ID
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

3 comments for “JPA JoinColumns Annotation Example

  1. Ben
    July 7, 2016 at 12:47 pm

    Can I use the same @JoinColumns annotation if I use an @IdClass as in example http://www.thejavageek.com/2014/05/01/jpa-idclass-example/ ? Or is there a difference in notation? I get exceptions that it can not find some logical names in the table.

  2. David
    November 18, 2016 at 10:16 pm

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

    Relationship “1 to Many” from TableA to Table B

    TABLE A
    ID (PK)
    COD (PK)
    YEAR (PK)
    MONTH (PK)

    TABLE B
    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
    @OneToMany(mappedBy=”TABLEA”)
    private Set TABLEB;

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

Leave a Reply

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