使用@ManytoOne批注和另一个referencedColumnName映射实体的重复列

时间:2017-02-06 19:00:55

标签: java hibernate

I have a problem with my mapping OneToMany. My exception is 

org.hibernate.MappingException:实体映射中的重复列:com.lcdut.model.SubjectsModel column:groups(应使用insert =“false”update =“false”映射)

@Entity
@Table(name = "subjects")
public class SubjectsModel {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(name = "groups")
    private int groups;
    ...........
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="groups", referencedColumnName="id_group")
    })
    private StudentsModel studentsModel;
    ........
}

@Entity
@Table(name = "students")
public class StudentsModel{
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Basic
    @Column(name = "pib")
    private String pib;
    @Basic
    @Column(name = "id_group")
    ........
    @OneToMany(mappedBy = "studentsModel")
    private Set<SubjectsModel> subjects;
}

1 个答案:

答案 0 :(得分:-1)

您已告诉Hibernate groups列的值来自原始属性groups以及关系映射studentsModel。这是非法的,只有一个可以影响价值。

您需要将groups映射指定为:

@Column(name = "groups", insertable = false, updatable = false)

或者您可以指定studentsModel是:

@JoinColumn(
  name = "groups", 
  referencedColumnName = "id_group", 
  insertable = false, 
  updatable = false
)

或最后删除原始属性并仅通过关系@ManyToOne映射来影响该值,而无需更改注释。