JPA: add should not fail if one of the rows cant be added
I use JPA + Hibernate.
I have 2 entities (Parent, Child) with OneToMany connec开发者_开发百科tion between them. I add a list of children to the parent like this:
parent.setChildrent(childrenSet);
getEntityManager().merge(parent);
If one of the children entities contains invalid values (for example, too long string), its insert fails, and the whole transaction fails.
Is there a way to ignore/log the error, and continue the merge for the rest of children?
I understand that I can add children one-by-one, but is there a better solution?
thank you.
If one of the children entities contains invalid values (for example, too long string),...
You shouldn´t handle problems like that by handle database errors. Try to filter too long strings or invalid values in your application before you start a transaction to the database.
If there is a database error a rollback is performed and all changes performed while this transaction will be undone.
Best regards
精彩评论