简体   繁体   中英

JPA join on legacy database non-primary key

I have the following tables in a legacy database:

Table1 
+ Table1ID PK
+ Table1Code

Table2
+ Table2ID PK
+ Table1Code

The convention is that there is only 1 Table1Code for each Table1ID. Table2 has a one to many relationship with Table1.

I want to reference them both as JPA objects. Is this possible using @OneToMany and specifying the @JoinColumn s?

Yes, you just have to use the referencedColumnName attribute of the JoinColumn annotation:

@OneToMany
@JoinColumn(name = "Table1Code", referencedColumnName = "Table1Code")
private List<Table2> table2s;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM