简体   繁体   English

如何使用EclipseLink更新MySQL表中的列?

[英]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.

相关问题 如何使用EclipseLink和Spring配置动态编织? - How do I configure dynamic weaving using EclipseLink & Spring? 当使用Java在MySql的不同表的不同列中进行插入时,如何自动更新另一个表中的列 - how to automatically update a column in another table when an insertion is made in a different column in a different table in MySql using java 如何使用Eclipselink创建Java枚举表? - How to create table of Java enum using Eclipselink? 如何使用EclipseLink在列中找到具有唯一值的值? - How do I find a value in a column that just have unique values with EclipseLink? 如何防止EclipseLink(JPA 2.0)清除数据库/表? - How do I prevent EclipseLink (JPA 2.0) from clearing my database/table? 如何使用JPA / EclipseLink与Junction Table建立多对多关系 - How do you Set Up a Many-to-Many Relationship with Junction Table using JPA/EclipseLink 使用带注释的JPA对象(Hibernate JPA提供程序)时,如何在列中插入MySQL表的默认日期时间值? - How do I insert MySQL table's default datetime value in a column when using annotated JPA objects (Hibernate JPA provider)? 如何使用JPA / EclipseLink从联结表中获取信息? - How can I get information out of a junction table using JPA/EclipseLink? JPA,Eclipselink,MySQL。 您如何查询null? - JPA, Eclipselink, MySQL. How do you query for null? 我如何使用休眠在mysql集群中创建表 - how do i create table in mysql cluster using hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM