There are 2 tables / classes:

public class Message { public Message() { } @Column @GeneratedValue @Id private Integer id; @Column(name = "recive_user_id") private String ReciveUserId; @Column(name = "send_user_id") private String SendUserId; @Column(name = "contain") private String contain; 

and

 public class UserProf { @Id @GeneratedValue @Column private Integer id; @Column(name = "name") private String name; @Column(name = "first_name") private String firstName; @Column(name = "city") private String city; @Column(name = "number") private int number; 

It is necessary to organize a one-to-one relationship between:

 id(UserProf(Primary Key)) и ReciveUserId(Message(FK)) id(UserProf(Primaty Key)) и SendUserId(Message(FK)) 

Tell me, how can this be done using annotations?

    1 answer 1

     public class Message { public Message() { } @Column @GeneratedValue @Id private Integer id; @OneToOne @PrimaryKeyJoinColumn @Column(name = "recive_user_id") private UserProf ReciveUserId; @OneToOne @PrimaryKeyJoinColumn @Column(name = "send_user_id") private UserProf SendUserId; @Column(name = "contain") private String contain 
    • one
      Although it’s better to use @JoinColumn (name = "recive_user_id", referencedColumnName = "id") instead of @PrimaryKeyJoinColumn - Denis Fedichkin
    • Do you need to make any changes in the UserProf table? - Heaven
    • No, it is not necessary to make - Denis Fedichkin
    • Initial SessionFactory creation failed.org.hibernate.AnnotationException: @Column (s) not allowed on a @OneToOne property: com.db.table.Message.ReciveUserId - Heaven
    • one
      Remove the @Column annotation, it seems confused with something - Denis Fedichkin