How to use the primary key defined in one @Entity class in another @Entity class in @OneToOne relationship in hibernate
Till now I've this...I do not want to use @Id in phoneNumber of the Phone class. How to do that? And are @JoinColumn and @MapsId annotations mandatory(what do they exactly do)? Thank you all.
@Entity
public class User {
@Id
private int id;
private String name;
@OneToOne
private Phone phone;
...
}
@Entity
public class Phone {
private int phoneNumber;
@OneToOne
@JoinColumn(name="id")
@MapsId
private User user;
...
}
Edited: while mapped to tables, I want the table Phone
to have a column that is a primary key for itself while being the same value as that of the primary key of table User
.