如何在包含对象集的对象插入列表期间忽略唯一违规 [英] How to ignore unique violation during insert list of objects which contain set of object

查看:47
本文介绍了如何在包含对象集的对象插入列表期间忽略唯一违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Hibernate中使用PostgreSQL nad和Spring数据JPA.我与orphanRemoval = false有关系OneToMany,因为我经常将许多孩子添加到关系中.

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.

父母:

@OneToMany(mappedBy ="parent",级联= {CascadeType.ALL},orphanRemoval =否,fetch = FetchType.LAZY)公共设置getChildren(){归还孩子;}

@OneToMany(mappedBy = "parent", cascade = { CascadeType.ALL }, orphanRemoval = false, fetch = FetchType.LAZY) public Set getChildren() { return children; }

孩子:

@ManyToOne@JoinColumn(name ="parent_id")公共家长getParent(){返回父母}

@ManyToOne @JoinColumn(name = "parent_id") public Parent getParent() { return parent; }

要保留或合并对象,请使用方法

To persist or merge object I use method

Iterable<T>保存(Iterable<扩展了T>实体)

Iterable< T > save(Iterable< extends T> entities)

形成CrudRepository.我保存父母名单,其中每个父母都包含一组孩子.子表具有唯一约束.如果发生约束违例,我想忽略它并省略(不要持久化)子项,这会导致出现振动,但是我想插入每个不出现约束违例的子项.该怎么做?

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?

推荐答案

通过异常处理此问题.

  1. 尝试更新数据库,如果此处没有问题.捕获UniqueViolationException并找到JDBCException.升级到合格的数据库异常并找到损坏的子代.

  1. 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.

从父母那里带走孩子.

转到1.

这篇关于如何在包含对象集的对象插入列表期间忽略唯一违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆