Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why hibernate creates a join table for unidirectional OneToMany?

Why hibernate uses a join table for these classes?

@Entity
public class CompanyImpl {
    @OneToMany
    private Set<Flight> flights;


@Entity
public class Flight {

I don't want neither a join table nor a bidirectional association:(

like image 819
morteza khosravi Avatar asked Dec 18 '12 21:12

morteza khosravi


People also ask

What is unidirectional mapping in Hibernate?

In Many-To-One Unidirectional mapping, one table has a foreign key column that references the primary key of associated table.By Unidirectional relationship means only one side navigation is possible (STUDENT to UNIVERSITY in this example).

What is join table in Hibernate?

When a join table is used in mapping a relationship with an embeddable class on the owning side of the relationship, the containing entity rather than the embeddable class is considered the owner of the relationship. If the JoinTable annotation is missing, the default values of the annotation elements apply.

What is one to many relationship in JPA?

The One-To-Many mapping comes into the category of collection-valued association where an entity is associated with a collection of other entities. Hence, in this type of association the instance of one entity can be mapped with any number of instances of another entity.

Can many to many be unidirectional?

A Course can have many Students and a Student can take many courses. This creates a Many to Many relationship. The following example shows a unidirectional Many to Many mapping in Hibernate.


1 Answers

Because that's how it's designed, and what the JPA spec tells it to map such an association. If you want a join column in the Flight table, use

@Entity
public class CompanyImpl {
    @OneToMany
    @JoinColumn(name = "company_id")
    private Set<Flight> flights;
}

This is documented.

like image 60
JB Nizet Avatar answered Oct 16 '22 07:10

JB Nizet