Search code examples
javahibernateormannotations

cannot insert null in one to many relationship hibernate annotation


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 hibernate annotations. When i save class A, it gives me error that cannot insert null B.a_id . A-id is not nullable in my database. How can i persist this relationship.


Solution

  • This is a unidirectional relationship from A->B. a_id column in table B is not nullable. When hibernate tries to save class B, it not able to find value for a_id.

    Well, did you try to make the JoinColumn non nullable?

    @OneToMany 
    @Cascade({CascadeType.ALL}) 
    @JoinColumn(name="A_ID", nullable=false)
    private Set<B> b;
    

    See also