更新JPA实体中的一个字段 [英] Updating one field in JPA entity
问题描述
我有一个实体User
,该实体在功能中分配了系统特权.我有一个网页,您可以在其中从下拉列表中选择一个用户,然后将表单提交到服务器.在服务器端,我只想为该实体更新一个字段.
I have an Entity User
that is assigned a system privilege in a function. I have a web page where you select a user from a drop down and submit the form to the server. On the server side I want to update only one field for that entity.
我的用户"实体将对所有对象具有默认值,但要设置的一个字段及其ID除外.我需要做一个findById
然后更新特定的字段然后进行合并吗?或者有没有办法告诉该用户只更新那个字段?
My User entity will have default values for all objects except the one field that gets set and its id. Do I need to do a findById
then update the specific field then do the merge or is there a way to tell the to only update that field?
推荐答案
我需要执行一次findById,然后更新特定字段,然后进行合并
Do I need to do a findById then update the specific field then do the merge
这是常规方法,只是不需要合并托管实体,只需让JPA检测到更改并自动更新即可.
That would be the regular approach, except that you don't need to merge a managed entity, just let JPA detect the changes and update it automatically.
还是有一种方法告诉只能更新该字段?
or is there a way to tell the to only update that field?
不.但是您可以使用User
的简短"版本(仅更新字段).另一个选择是使用批量更新操作,但这不是IMO真正的好用例.我不会使用这种方法.
No. But you could use a "short" version of your User
(with only the field(s) to update). Another option would be to use a Bulk Update Operation but this is IMO really not a good use case. I wouldn't use this approach.
- JPA 1.0规范
- 4.10批量更新和删除操作
- JPA 1.0 specification
- 4.10 Bulk Update and Delete Operations
这篇关于更新JPA实体中的一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!