JPA OneToMany和ManyToOne抛出:实体列映射中的重复列(应使用insert ="false" update ="false"进行映射) [英] JPA OneToMany and ManyToOne throw: Repeated column in mapping for entity column (should be mapped with insert="false" update="false")

查看:327
本文介绍了JPA OneToMany和ManyToOne抛出:实体列映射中的重复列(应使用insert ="false" update ="false"进行映射)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个类,名称之一是User,此用户有其他类实例.像这样;

I have three classes one of the name is User and this user have 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;
 }

它像这样工作,但是在db中生成emty表.其中必须包含外键.当我尝试使用mapledBy和JoinColumn通告时,我失败了.我该如何解决?

it's working like this but generates emty tables in db. Which have to contains foreign keys. When I tried to use mappedBy and JoinColumn annotains I got failed. How can I resolve this?

其他信息:

当我改变时;

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

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

我要

发生JPA错误(无法建立EntityManagerFactory):实体的映射中重复的列:models.post.APost列:id(应使用insert ="false" update ="false"进行映射)

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")

最终最后,关于jpa注释,我完全错了. :(当我改变

Final Finally, I was totaly wrong about jpa annotaions. :( When i change

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

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

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

一切正常. :)

推荐答案

正如我在高性能Java持久性,您永远不应使用单向@OneToMany注释,因为:

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

  1. 它会生成效率低下的SQL语句
  2. 它会创建一个额外的表,从而增加数据库索引的内存占用量

现在,在您的第一个示例中,双方都拥有该关联,并且这很糟糕.

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

虽然@JoinColumn将让@OneToMany一方负责关联,但这绝对不是最佳选择.因此,请始终在@OneToMany侧使用mappedBy属性.

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

这篇关于JPA OneToMany和ManyToOne抛出:实体列映射中的重复列(应使用insert ="false" update ="false"进行映射)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆