JPA reference table mapping

走远了吗. 提交于 2019-12-12 10:13:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!