user3552178
user3552178

Reputation: 2971

Java hibernate OneToOne: which side should be the owner

So i'm learning from these simple examples, there're 2 tables, USERS and USER_DETAILS, simple enough, each user has user_details and it's 1-to-1 relationship. So this sample is like this,

@Entity
@Table(name = "USERS")
public class User {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "USR_ID")
   private long id;

   @Column(name = "USERNAME", nullable = false, unique = true)
   private String username;

   @Column(name = "PASSWORD")
   private String password;

   @OneToOne(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
   private UserDetail userDetail;

   //Setter and getter methods
}

@Entity
@Table(name = "USER_DETAILS")
public class UserDetail {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "USR_DET_ID")
   private long id;

   @Column(name = "FIRST_NAME")
   private String firstName;

   @Column(name = "LAST_NAME")
   private String lastName;

   @Column(name = "EMAIL")
   private String email;

   @Column(name = "DBO")
   private LocalDate dob;

   @OneToOne(fetch=FetchType.LAZY)
   @JoinColumn(name = "USR_ID")
   private User user;

   //Setter and Getter methods
}

If you look at mappedBy, it's in the User not UserDetails.

Q1: so USER is the owner, if it calls save(), 
USER_DETAILS table will be updated as well ?

Q2: same examples put mappedBy in the USER_DETAILS side,
why people want to do this ?
How to determine which side to put mappedBy ? 

Thanks for your help !

Upvotes: 0

Views: 1925

Answers (1)

Hülya
Hülya

Reputation: 3433

Q2: same examples put mappedBy in the USER_DETAILS side, why people want to do this ? How to determine which side to put mappedBy ?

In a bidirectional relationship, each entity has a relationship field or property that refers to the other entity. Through the relationship field or property, an entity class’s code can access its related object. If an entity has a related field, the entity is said to “know” about its related object.

There is a bidirectional one-to-one relationship in your example. Both User and UserDetail entities have a relationship field. @OneToOne annotation specified on both the entities.

For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key.

The owner of the relationship is UserDetail entity. The owner has @JoinColumn annotation to specify foreign key (USR_ID).

Inverse side of relationship (User) has mappedBy attribute.

Q1: so USER is the owner, if it calls save(), USER_DETAILS table will be updated as well ?

In your example UserDetail is the owner. Therefore the saving process:

User user = new User();  // Ignoring the constructor parameters...
UserDetail userDetail = new UserDetail();  

user.setUserDetail(userDetail);
userDetail.setUser(user);

userRepository.save(user);

You only need to save the parent. It will save the child as well.

Upvotes: 1

Related Questions