开发者

JPA: add should not fail if one of the rows cant be added

开发者 https://www.devze.com 2023-03-12 23:25 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消