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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JPA OneToMany and ManyToOne throw: Repeated column in mapping for entity column (should be mapped with insert=”false” update=”false”) without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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, cascade = CascadeType.ALL)
    public List<APost> aPosts;

    @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
    public List<BPost> 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;
 }

it’s working like this but generates empty tables in DB. Which have to contain foreign keys. When I tried to use mappedBy and JoinColumn annotations I got failed. How can I resolve this?

Extra information:

When I changed with;

 @ManyToOne(fetch=FetchType.LAZY)
 @JoinColumn(name="id")
 public User user;

and

 @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="id")
 public List<APost> aPosts;

I’m getting

A JPA error occurred (Unable to build EntityManagerFactory): Repeated column in mapping for entity: models.post.APost column: id (should be mapped with insert=”false” update=”false”)

Final Edit: Finally, I was totally wrong about JPA annotations. 🙁 When I change

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="id")

to

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="user")

and

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_id")

everything works ok. 🙂

Answer

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<APost> aPosts;

    @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="user")
    public List<BPost> 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;
}
We are here to answer your question about JPA OneToMany and ManyToOne throw: Repeated column in mapping for entity column (should be mapped with insert=”false” update=”false”) - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji