问题
There are two main @Entity
classes reflecting these tables:
TableA {id,name}
TableB {id,name}
And one reference table
TableC {tableA.id,tableB.id}
Question is: how to map a TableA
's entity's field with @OneToMany
realation to TableB objects list:
@OneToMany
??????????
private List<TableBEntity> tableBItems;
回答1:
If what you really have is a OneToMany (which means that a give tableB.id
appears at most once in TableC
), then the mapping is the following:
@OneToMany
@JoinTable(name = "TableC",
joinColumns = @JoinColumn(name = "TABLE_A_ID"),
inverseJoinColumns = @JoinColumn(name = "TABLE_B_ID"))
private List<TableBEntity> tableBItems;
Else, what you have is in fact a ManyToMany, and the mapping is the same, except that @OneToMany
must be replaced by @ManyToMany
.
来源:https://stackoverflow.com/questions/11466593/jpa-reference-table-mapping