使用JPA处理集合更新 [英] Handling collection updates with JPA

查看:615
本文介绍了使用JPA处理集合更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义对象集合,我想使用JPA以最简单的方式持久化。
我的 CustomObject 类映射到一个表。现在我想知道JPA是否提供某种设施来处理对象集合。

I have a collection of custom objects and I'd like to persist it in the easiest way using JPA. My CustomObject class is mapped to a table. Now I'm wondering if JPA provides some kind of facility to handle object collection.

特别是我想添加或删除对象到我的集合,然后传递给a save(Collection< CustomObject> cco)方法,而不检查哪些对象被更改,我必须添加和删除...

In particular i'd like to add or remove objects to my collection and then pass to a save(Collection<CustomObject> cco) method without checking which object are changed, which I have to add and which to remove...

是否可能?

推荐答案

如果你正在寻找 EntityManager ,这将保存或更新集合,答案是否定的。您必须循环并执行保存或更新。

If you are looking for something in EntityManager which will save or update a collection, the answer is no. You will have to loop and do a save or update.

如果您使用 EntityManager.merge() API将更新,如果记录已存在或将插入一个新记录。

If you use EntityManager.merge() API it will update if record is existing or else will insert a new record.

就删除而言,JPA或任何其他ORM如何确定要删除该实例,如果它的软删除,列如 is_active true / false ,那么yes通过调用merge可以完成,但如果你想要一个硬删除然后JPA将无法自己确定,您将必须手动进行。

As far as delete is concerned, how will JPA or any other ORM determine that you want to delete that instance, if its a soft delete like updating a column like is_active to true/false, then yes by calling merge it can be done, but if you want a hard delete then JPA won't be able to determine by itself, you will have to do it manually.

这篇关于使用JPA处理集合更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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