JPA MapsId 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.

MapsId annotation in JPA is used in ManyToOne or OneToOne relationships when mapping using EmbeddedId is involved. Let us see a JPA MapsId example to demonstrate the same.

JPA MapsId Example:

Consider we have following.

  • Employee entity with fields idEmployee and name
  • Dependent entity with fields id and Employee where they are of type DependentId and Employee respectively.
  • DependentId is Embeddable class which is used as EmbeddedId in Dependent entity.

Now we have database table structure as below.

JPA MapsId Example

Notice that there are 3 java classes but only 2 entities as DependentId is an Embeddable which is used a EmbeddedId for Depedent entity. EMPLOYEE_ID column in dependent table should map to IDEMPLOYEE column in employee table. To fulfil this requirement, we have to create entitities for JPA MapsId example.

Classes for JPA MapsId Example:

Employee entity:

 DependentId Embeddable:

 Dependent Entity:

Notice that

  • We are creating ManyToOne relationship between Dependent and Employee. 
  • Name of database column in dependent table is EMPLOYEE_ID hence, we used  @JoinColumn(name = "EMPLOYEE_ID") .
  • We want to use the same employee id for Dependent entity that is being used in DependentId embeddable. Hence annotated with  MapsId("empId") .  It means that value of empId field is to be stored as foreign key.

Let us test this JPA MapsId example.

Run the program and you’ll get database populated as below.

Employee Table:

IDEMPLOYEE NAME
1 Prasad Kharkar

 Dependent Table:

Name EMPLOYEE_ID
Sushant 1

I hope this JPA MapsId example helped. Feel free to provide suggestions. Happy learning 🙂

 

Share Button

12 comments for “JPA MapsId example

  1. December 23, 2014 at 9:19 am

    Excellent article. Thanks a lot for sharing.

  2. March 12, 2015 at 10:32 am

    Hi Prasad,

    Thanks for providing this example, It was very useful to understand composite key concept. But when I am trying to run the same, only employee entity is getting saved not the dependent one. Could you please provide any other information on same.

    thanks

  3. Jay
    November 9, 2015 at 9:24 am

    I have exactly the same situation as in this example. Though I have checked to have the things the same as your code, after persist(employee), I have auto-generated IDEMPLOYEE (PK) value, but the empId (the FK in the dependent table) value is always 0. I am using Spring 3.2.8, hibernate 3, RAD, and Oracle database. I can just explicitly set the FK value and then persist the dependent table. But I don’t like it because it doesn’t seem to be hibernate’s way, and I am puzzled what can be wrong. Any suggestion will be very appreciated.
    Thanks a lot.

  4. Milan
    November 20, 2015 at 8:51 pm

    Not well explained. I still don’t get it for what “@MapsId” is used for??

    • August 16, 2017 at 8:57 pm

      @MapsId(“xxx”) Entity aEntity;

      says that aEntity is mapped by “xxx” column in the embedded id.

  5. sarfaraz
    December 30, 2015 at 10:30 am

    Hi ,

    Thnak you so much. I was struggling with this issue from last 4 days. Finally your example help me alot.

    Thank you once again

  6. Abhash Jain
    September 1, 2016 at 6:19 pm

    when i had tried your example in my setup than i am getting three column in dependent table. There is extra empid field.i am using oracle toplink to connect to the database. i have the same requirement. can you please help me ASAP.
    i have following jars.
    toplink-essentials-2.1-60f.jar
    toplink-essentials-agent-2.1-60f.jar
    javax.persistence-2.0.5.jar
    mysql> desc dependent;
    +————-+————–+——+—–+———+——-+
    | Field | Type | Null | Key | Default | Extra |
    +————-+————–+——+—–+———+——-+
    | EMPID | int(11) | NO | PRI | NULL | |
    | NAME | varchar(255) | NO | PRI | NULL | |
    | EMPLOYEE_ID | int(11) | YES | MUL | NULL | |
    +————-+————–+——+—–+———+——-+
    3 rows in set (0.00 sec)

    mysql> desc employee;
    +————+————–+——+—–+———+——-+
    | Field | Type | Null | Key | Default | Extra |
    +————+————–+——+—–+———+——-+
    | IDEMPLOYEE | int(11) | NO | PRI | NULL | |
    | NAME | varchar(255) | YES | | NULL | |
    +————+————–+——+—–+———+——-+
    2 rows in set (0.01 sec)

    mysql> desc id_gen;
    +———-+—————+——+—–+———+——-+
    | Field | Type | Null | Key | Default | Extra |
    +———-+—————+——+—–+———+——-+
    | gen_name | varchar(50) | NO | PRI | NULL | |
    | gen_val | decimal(38,0) | YES | | NULL | |
    +———-+—————+——+—–+———+——-+
    2 rows in set (0.14 sec)

    mysql> select * from dependent;
    +——-+———+————-+
    | EMPID | NAME | EMPLOYEE_ID |
    +——-+———+————-+
    | 0 | Sushant | 1 |
    +——-+———+————-+
    1 row in set (0.01 sec)

    mysql> select * from employee;
    +————+—————-+
    | IDEMPLOYEE | NAME |
    +————+—————-+
    | 1 | Prasad Kharkar |
    +————+—————-+
    1 row in set (0.00 sec)

  7. Horelvis
    September 17, 2016 at 4:50 pm

    Thanks you! very useful to me
    This relation type between entity is very common in big projects with complex database schema.

  8. Antonio Santos
    March 14, 2017 at 9:05 am

    Hi,

    This works when an EmbeddableId references a simple key. My scenario is a bit more farfetched. I want a field in entity A’s compound key to reference a field in entity B’s compound key. I’m not getting there and have tried a lot of things (from JoinColumn to MapsId and dot notation fields.
    What I’m trying to do is:
    A.ID.field1 —-> B.ID.field2.
    Can you help me? I’m going nuts 😉

    Thank you.

    Regards

    • March 14, 2017 at 11:13 am

      I will look into it and try to get back to you as soon as possible.

  9. Carlos
    August 9, 2017 at 12:29 am

    org.hibernate.PersistentObjectException: detached entity passed to persist: com.domain.mapsid.Employee

Leave a Reply

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