Search code examples
hibernatehibernate-onetomany

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:(


Solution

  • 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.