Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

No you do not need a join table for OneToMany. Look at the @mappedBy annoatation

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top