Reputation: 3728
I have 2 tables:
A
s_id(key) name cli type
B
sa_id(key) s_id user pwd
So in Jpa I have:
@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(){...}
}
class b is just a basic entity class with no reference to A.
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?
Upvotes: 2
Views: 842
Reputation: 2499
The quick answer is that if you have a Many-to-Many relationship you will need another table. If you have a One-to-Many or a Many-to-One relationship you will not.
Upvotes: 0
Reputation: 103135
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(){...} }
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.
Upvotes: 1
Reputation: 16809
No you do not need a join table for OneToMany. Look at the @mappedBy annoatation
Upvotes: 2