使用@ManyToOne hibernate-mapping,引用列无法正常工作

时间:2017-02-07 08:44:14

标签: java hibernate-mapping

我的映射OneToMany有问题。我的例外是 org.hibernate.MappingException:实体映射中的重复列:com.lcdut.model.SubjectsModel column:groups(应使用insert =&#34映射; false&#34; update =&#34; false&#34;)< / p>

如果我使用udpatetable或插入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;
}

2 个答案:

答案 0 :(得分:0)

@Column(name = "groups")
private int groups;
...........
@ManyToOne
@JoinColumns({
    @JoinColumn(name = "groups", referencedColumnName = "id_group")
})
private StudentsModel studentsModel;
........

这两个字段由同一列映射。您不需要private int groups;。删除它,一切都会工作。

答案 1 :(得分:0)

如果我理解正确的方法,它应该是1:n-relation,它应该像下面这样工作(列/外键studentid必须作为数据库中的int存在):

    @Entity
    @Table(name = "subjects")
    public class SubjectsModel {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;

        @ManyToOne
        @JoinColumn(name = "studentsid")
        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;
        @OneToMany(mappedBy = "studentsModel")
        private Set<SubjectsModel> subjects;
    }