JPA @OneToMany

Ariel Luduena Karhs
October 18, 2010 | 2 min read

Suppose you have this two entities: PhoneList and Phone.

PhoneList have a @OneToMany to Phone. (I need only a unidirectional relationship)

Something like:

public List<Phone> getPhones() {
return phones;

The phone entity doesn’t have nothing special.

If you deploy those entities your JPA provider will create 3 tables. One table for each entity and also a “JoinTable”. But, why? This kind of relationship can be done by 2 tables only and I don’t want a third table. I want a PHONE table with an FK column to PHONELIST table.

How can I do that?

Two options:

  1. Make your relationship bidirectional
  2. Add an @JoinColumn in the @OneToMany attribute

Option number 1 – Make your relationship bidirectional

The problem is that “I don’t need a bidirectional relationship”! … Ok, but, suppose that you are more interested in solve this and move forward to a “real issue”.

To make this relationship bidirectional, you must add in the Phone entity the reference to the PhoneList entity with the @ManyToOne annotation. Something like:

Also you have to add to the relationship @OneToMany in the PhoneList entity the mapped by attribute. Something like this:

Option number 2 – Add @JoinColumn in the @OneToMany attribute

If you don’t want to have a bidirectional relationship, you have to use @JoinColumn like this:

and the Phone entity remain without references

Get our stories delivered to your inbox weekly.