Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Many-to-One mapping with not null constraint

I have a many-to-one mapping and not null constraint on the column. i am getting the exception column is Null while saving. Can anybody please suggest how do i use cascade and inverse in this scenario?

like image 785
user1448876 Avatar asked Nov 15 '25 16:11

user1448876


2 Answers

Instead of @NotNull, indicate that the association is not optional using @ManyToOne(optional = false). The @NotNull is not specific to Hibernate; it's a generic bean annotation that may be enforced prior to saving the entitites. To be safe, I suggest using @NotNull primarily for @Column attributes.

like image 190
Paulo Merson Avatar answered Nov 18 '25 19:11

Paulo Merson


the entity with the onetomany/collection will by default maintain the association by cascading to the child which inserts first without the backreference and then updates the id. To solve this the onetomany/collection mapping must have the inverse attribute set and the backreference in the child must be set correctly.

like image 25
Firo Avatar answered Nov 18 '25 19:11

Firo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!