Utku
Utku

Reputation: 2177

Why does Hibernate create a Join Table by default for @OneToMany?

@OneToMany annotation, by default, creates a join table, unless the mappedBy element is specified.

What is the reason for this behaviour? For example, with the following entities:

@Entity
public class User {
    // ...
    @OneToMany
    private List<UserDocument> documents;
    // ...
}

@Entity
public class UserDocument {
    // ...
    @ManyToOne
    private User user;
    // ...
}

For the User entity, why doesn't Hibernate simply:

  1. Find the field with type User in UserDocument by doing reflection on UserDocument entity.
  2. Infer the value of mappedBy for the @OneToMany annotation by itself?

What is the reason for not doing this and generating a join table as the default behaviour? Why is Hibernate (or JPA) is designed this way?

Upvotes: 1

Views: 1206

Answers (1)

Jack Q
Jack Q

Reputation: 311

A simple reason behind this is that Hibernate cannot known for sure that a filed of type User inside of UserDocument is corresponding to the specific User-UserDocument relation. Without a mappedBy property, Hibernate can only create a join table or insert a generated column in UserDocument table. However, the latter alters data model and introduces more problem than it may resolve ( distinguish generated or declared column; table schema mismatch model class; etc.). Thus Hibernate use a join table to store the mapping.

For example, if you want to track the last one who modifies a document, you may need another many-to-one relation in UserDocument. This cannot be infered and resolved just using reflection.

@Entity
public class UserDocument {
    // ...
    @ManyToOne
    private User user;

    @ManyToOne
    private User lastModifiedBy;
    // ...
}

Upvotes: 6

Related Questions