I have a question regarding mapping entities based on mapping in their tables. So, we are a team of five working on a project, one of our team mate seem to add mapping between tables in opposite direction, I'll give examples of both to let you understand what I mean.
We have two tables User and UserInfo. UserInfo has a user_id as foreign key.
1) Common Mapping I have learnt about in hibernate.
In User and UserInfo entities I usually have mappings like this:
class User{
private int userId;
private String userName;
// getter and setters
}
class UserInfo{
private int userInfoId;
private String firstName;
private String lastName;
@OneToOne()
@JoinColumn(name="user_id")
private User user;
}
2) This is how my colleague does mapping:
class User{
private int userId;
private String userName;
@OneToOne(mappedBy="user")
@JoinColumn(name="user_id")
private UserInfo userInfo;
// getter and setters
}
class UserInfo{
private int userInfoId;
private String firstName;
private String lastName;
@OneToOne()
private User user;
}
He does just opposite of what I learnt from tutorials. It is working fine somehow but I am not sure if this is the right way to map two entities. Please help.
Thanks.