简体   繁体   中英

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?

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;
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM