JPA OneToMany and ManyToOne throw: Repeated column in mapping for entity column (should be mapped with insert=“false” update=“false”)

后端 未结 2 1458
逝去的感伤
逝去的感伤 2020-11-29 21:22

I have three classes one of the names is User and this user has other classes instances. Like this;

public class User{
    @OneToMany(fetch=FetchType.LAZY, ca         


        
2条回答
  •  佛祖请我去吃肉
    2020-11-29 21:30

    As I explained in this article and in my book, High-Performance Java Persistence, you should never use the unidirectional @OneToMany annotation because:

    1. It generates inefficient SQL statements
    2. It creates an extra table which increases the memory footprint of your DB indexes

    Now, in your first example, both sides are owning the association, and this is bad.

    While the @JoinColumn would let the @OneToMany side in charge of the association, it's definitely not the best choice. Therefore, always use the mappedBy attribute on the @OneToMany side.

    public class User{
        @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="user")
        public List aPosts;
    
        @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="user")
        public List bPosts;
    }
    
    public class BPost extends Post {
    
        @ManyToOne(fetch=FetchType.LAZY)    
        public User user;
    }
    
    public class APost extends Post {
    
         @ManyToOne(fetch=FetchType.LAZY) 
         public User user;
    }
    

提交回复
热议问题