Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

java - JPA @JoinColumn issues while joining on non primary key columns

I want to connect table based on one direction join, here is my code:

Class Person {

@id
String person_sk;

int person_id;

String Person_name;

@OneToMany
@joinColumn (name="person_reference_id")
List<address> getAddresses() {}

}

class Address
{

@id
int person_reference_id (referred from Person);

@id
int address_id;

@id
int phone_id;

String street_name, zip_code;

}

Now when I do getAddress, it does not work, because my join is based on person_ref_id and @id (primaryKey) column in Person class is person_sk.

If I use referencedColumn then also it doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. JPA does not allow references to non-PK columns.

  2. ReferencedColumn property is used to specify join column, when there is a composite PK in referenced table.

How can I make this work??

Normalize your database to use common PK-FK relationships. If needed - define composite PK.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...