I use PostgreSQL nad Spring data JPA with Hibernate. I have relation OneToMany with orphanRemoval = false because I very often add many childs to relation.
Parent:
@OneToMany(mappedBy = "parent", cascade = { CascadeType.ALL }, orphanRemoval = false, fetch = FetchType.LAZY) public Set getChildren() { return children; }
Child:
@ManyToOne @JoinColumn(name = "parent_id") public Parent getParent() { return parent; }
To persist or merge object I use method
Iterable< T > save(Iterable< extends T> entities)
form CrudRepository. I save list of parents, where every parent contain set of child. Child table has unique constraint. If constraint violations occurs I want to ignore that and ommit (do not persist) child which cases viloations but I want insert every child which doesn't case constraint violation. How to do that?
Handle this dirty by Exceptions.
Try to Update the Database, if fine break here. Catch the UniqueViolationException and find the JDBCException. Upcast to your qualified Database Exception and find the broken Children.
Remove the Children from the Parent.
Go to 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With