How to make OneToOne relationship using join on secondary key?

I have two classes:

@Entity
@Table(name = "clients")
public class Client extends Model {

    @Id
    public int id;

    public String name;

    @OneToOne
    public Contact contact;
}

@Entity
@Table(name = "contacts")
public class Contact extends Model {

    @Id
    public int id;

    @OneToOne
    @Column(name = "client_name")
    public String clientName;
}

      

Now I want to establish a OneToOne relationship, but I am joining the client_name ( on clients.name=contacts.client_name

) column . How to do it?

I know it's better to use the primary key, but the database structure is fixed and I cannot change it.

+3


source to share


1 answer


Try:



@OneToOne
@Column(name = "client_name")
@JoinColumn(name = "client_name", referencedColumnName = "name")
public Client client;

      

0


source







All Articles