JPA和2个简单表 [英] JPA and 2 simple tables
问题描述
我有2张桌子:
A
s_id(key) name cli type
B
sa_id(key) s_id user pwd
所以在Jpa 我有:
@Entity
class A...{
@OneToMany(fetch=FetchType.EAGER)
@JoinTable( name="A_B",
joinColumns={@JoinColumn(name="a_id", table="a",unique=false)},
inverseJoinColumns={@JoinColumn(name="b_id", table="b", unique=true)} )
Collection<B> getB(){...}
}
b类只是一个基本实体类,没有对A的引用.
class b is just a basic entity class with no reference to A.
希望这很清楚.我的问题是:我真的需要一个联接表来进行这种简单的联接吗?不能使用简单的joincolumn或其他方法完成此操作吗?
Hopefully that is clear. My question is: Do I really need a join table to do such a simple join? Can't this be done with a simple joincolumn or something?
推荐答案
您不需要JoinTable.如果B类没有引用A类,则满足以下条件
You do not need a JoinTable for this. If the class B has no reference to class A then the following will suffice
@Entity class A...{
@OneToMany(fetch=FetchType.EAGER)
Collection getB(){...} }
在大多数情况下,尽管您可能需要双向关系,在这种情况下B引用了A.在这种情况下,您将需要查找@mappedBy批注.保罗提到的.
In most cases though you may want a bidirectional relationship in which case B has a reference to A. In that case you will need to look up the @mappedBy annotation. mentioned by Paul.
这篇关于JPA和2个简单表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!