[英]How do I update a column in a table in MySQL using EclipseLink?
I am wondering how I can update a column in a table with EclipseLink. 我想知道如何使用EclipseLink更新表中的列。 I tried using EntityManager.merge(myObject);
我尝试使用EntityManager.merge(myObject);
but then EclipseLink throwed some exceptions saying it coudln´t update the other columns with a null value. 但是随后EclipseLink引发了一些异常,说它无法用空值更新其他列。 I tried to add the @ChangeTracking
property to my class I want to update so it just updates the column that has been changed, but it didn´t work and I even don´t know if that is the correct way to do it. 我试图将@ChangeTracking
属性添加到我想更新的类中,以便它仅更新已更改的列,但是它不起作用,我什至不知道这是否是正确的方法。
So how can I update just a single column in a table using EclipseLink? 那么,如何使用EclipseLink更新表中的单个列?
If your merge isn't working, it is likely because you are creating a new instance from scratch and only attempting to populate the value you want to change. 如果合并无效,则可能是因为您是从头开始创建新实例,而仅尝试填充要更改的值。 This results in merge overwriting all the other fields with null. 这导致合并覆盖所有其他字段为null。 Do not use em.merge on incomplete entities since it merges every JPA managed field - it has no way of knowing what you intended and must merge the entire state of the entity. 不要在不完整的实体上使用em.merge,因为它会合并每个JPA管理的字段-它无法知道您的意图,并且必须合并实体的整个状态。
Instead use your changes to modify the managed instance. 而是使用您的更改来修改托管实例。 A simple example: 一个简单的例子:
em.getTransaction().begin();
YourEntity instance = em.find(YourEntity.class, id);
instance.setYourAttribute(newValue);
em.getTransaction().commit();
Or a detached instance read in from an EM that you later use em.merge on. 或从EM读取的分离实例,以后再使用em.merge。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.