6

I am trying to understand what difference the @MapsId annotation makes in Hibernate. I have gone through the Hibernate documentation but I am still confused on the explanation given there as I am new to Hibernate.

The document says this:

@Entity
class Customer {
   @EmbeddedId CustomerId id;
   boolean preferredCustomer;

   @MapsId("userId")
   @JoinColumns({
      @JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
      @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
   })
   @OneToOne User user;
}

@Embeddable
class CustomerId implements Serializable {
   UserId userId;
   String customerNumber;

   //implements equals and hashCode
}

@Entity 
class User {
   @EmbeddedId UserId id;
   Integer age;
}

@Embeddable
class UserId implements Serializable {
   String firstName;
   String lastName;

   //implements equals and hashCode
}

In the embedded id object, the association is represented as the identifier of the associated entity. But you can link its value to a regular association in the entity via the @MapsId annotation. The @MapsId value correspond to the property name of the embedded id object containing the associated entity's identifier. In the database, it means that the Customer.user and the CustomerId.userId properties share the same underlying column (user_fk in this case).

I am not clear on what this explanation says, so I tired with hbm2ddl set as create in my configuration file and I observed these:

With @MapsId and @JoinColumns on Customer entity the DDL statements are:

Hibernate: create table CUSTOMER (customerNumber varchar2(255 char) not null, preferredCustomer number(1,0) not null, userfirstname_fk varchar2(255 char) not null, userlastname_fk varchar2(255 char) not null, primary key (customerNumber, userfirstname_fk, userlastname_fk))
Hibernate: create table USER (firstName varchar2(255 char) not null, lastName varchar2(255 char) not null, age number(10,0), primary key (firstName, lastName))
Hibernate: alter table CUSTOMER add constraint UK_xxxx  unique (userfirstname_fk, userlastname_fk)
Hibernate: alter table CUSTOMER add constraint FK_xxxx foreign key (userfirstname_fk, userlastname_fk) references TBL_USER

If I remove the @MapsId and @JoinColumns annotations I see these:

Hibernate: create table CUSTOMER (customerNumber varchar2(255 char) not null, firstName varchar2(255 char), lastName varchar2(255 char), preferredCustomer number(1,0) not null, user_firstName varchar2(255 char), user_lastName varchar2(255 char), primary key (customerNumber, firstName, lastName))
Hibernate: create table USER (firstName varchar2(255 char) not null, lastName varchar2(255 char) not null, age number(10,0), primary key (firstName, lastName))
Hibernate: alter table CUSTOMER add constraint FK_xxxx foreign key (user_firstName, user_lastName) references TBL_USER

Please help me in understanding the concept of the @MapsId annotation in Hibernate.

1 Answer 1

5

@MapsId will share main entity's primary key as the child entity's primary key no need to insert foreign key in child entity. Hibernate will map both entity's PK and return us a single object.

2
  • Thanks Kanagaraj, can you please tell me which one is considered as main entity and which is child entity?
    – Chaitanya
    Aug 12, 2014 at 18:18
  • In your case customer is main and user is child entity. Aug 13, 2014 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.