JPA annotations for entity mapping

82 Views Asked by At

I have two entities User and Contact . User object contains the Contact. How do I use JPA annotations so that when I save User object , the contact table should have Users's id as its primaryKey ?

            Public class User{
            public String username;
            public long id;
            public Contact contact;

            }

            public class Contact {
            public long id;
            public string phone;
            public string email;
            }
1

There are 1 best solutions below

3
On

Something similar to this

@Entity  
public class User{
    @Column
    public String username;
    @Id
    public long id;
    @OneToMany(mappedBy="id")
    public Contact contact;

}

@Entity 
public class Contact {
    @Id
    public long id;
    @Column
    public string phone;
    @Column
    public string email;
}