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 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?
-
No you do not need a join table for OneToMany. Look at the @mappedBy annoatation
From Paul Whelan -
You may want to reformat your post. (4 spaces before a line makes it look like code).
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.
From Steve g -
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.
From Vincent Ramdhanie
0 comments:
Post a Comment