hibernate onetone with mapsid

hibernate onetone with mapsid

The @OneToOne annotation is used to define a one-to-one relationship between two entities in Hibernate. The @MapsId annotation is used to specify that the primary key of the parent entity should be used as the foreign key in the child entity.

Here's an example of how to use the @OneToOne and @MapsId annotations in a Hibernate entity:

@Entity
public class Employee {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Long id;

  @OneToOne(mappedBy = "employee")
  @MapsId
  private EmployeeProfile employeeProfile;

  // other fields and methods
}

@Entity
public class EmployeeProfile {
  @Id
  private Long id;

  @OneToOne
  @JoinColumn(name = "id")
  private Employee employee;

  // other fields and methods
}
Sour‮c‬e:www.lautturi.com

In this example, the Employee entity has a one-to-one relationship with the EmployeeProfile entity. The @MapsId annotation on the employeeProfile field specifies that the id field of the Employee entity should be used as the foreign key in the EmployeeProfile entity.

The @JoinColumn annotation on the employee field in the EmployeeProfile entity specifies the name of the column in the EmployeeProfile table that should be used to store the foreign key.

Created Time:2017-11-01 12:05:07  Author:lautturi