JPA引用表映射 [英] JPA reference table mapping

查看:160
本文介绍了JPA引用表映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个主要的 @Entity 类反映了这些表:

There are two main @Entity classes reflecting these tables:

TableA {id,name}
TableB {id,name}

和一个参考表

TableC {tableA.id,tableB.id}

问题是:如何使用 @OneToMany TableA 的实体字段c $ c>对TableB对象列表的反映:

Question is: how to map a TableA's entity's field with @OneToMany realation to TableB objects list:

@OneToMany
??????????
private List<TableBEntity> tableBItems;


推荐答案

如果您真正拥有的是OneToMany(这意味着在 TableC 中,给出 tableB.id 最多出现一次,然后映射如下:

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;

否则,你所拥有的实际上是ManyToMany,并且映射是相同的,除了 @OneToMany 必须由 @ManyToMany 替换。

Else, what you have is in fact a ManyToMany, and the mapping is the same, except that @OneToMany must be replaced by @ManyToMany.

这篇关于JPA引用表映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆