cannot insert null in one to many relationship hibernate annotation

前端 未结 2 808
渐次进展
渐次进展 2021-01-01 03:31

I have a class A{Set b .....} which holds references of class B as Set. It is one to many relationship. Both class have sequencer in oracle. I put cascade to all in hibernat

2条回答
  •  耶瑟儿~
    2021-01-01 04:26

    I ran into the same problem and solved it by using the mappedBy attribute of the @OneToMany annotation in Class A:

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "m_a")
    private Set b;
    

    Here, m_a is the field in Class B that refers to Class A:

    @JoinColumn(name = "aId", nullable = false)
    @ManyToOne
    private A m_a;
    

    This way, the @JoinColumn is only specified in one place, no duplicated code.

提交回复
热议问题