简体   繁体   English

EJB实体:更新单个列的最佳方式

[英]EJB entities: Best way to update a single column

Using EJB entities, I need to update a single column of a single row in a 100+ columns table. 使用EJB实体,我需要更新100+列表中单行的单个列。 I usually read/load the row and make necessary updates, but that seems overwhelming as all remaining columns will be updated unnecessarily, not to mention the performance penalty. 我通常读取/加载行并进行必要的更新,但这似乎是压倒性的,因为所有剩余的列将不必要地更新,更不用说性能损失了。 Is there a way to just update the single column similar to the traditional JDBC way? 有没有办法像传统的JDBC方式一样更新单列?

You can execute an update query: 您可以执行更新查询:

update Foo f set f.someField = :someValue where f.id = :id

Be aware though that this change won't be reflected in the entity if it's already loaded, and that it will bypass optimistic locking. 请注意,如果实体已经加载,则此更改不会反映在实体中,并且它将绕过乐观锁定。 Make sure not to optimize prematurely. 确保不要过早优化。 If simply modifying the entity is fast enough, then just do that. 如果只是修改实体足够快,那就这样做吧。

I would rather feel concerned about having an entity with more than 100 fields. 我宁愿担心拥有超过100个字段的实体。 Maybe you should split that into several entities. 也许你应该把它分成几个实体。

use JPA, first you can query the record entity need to be updated,and modify the properties of this entity. 使用JPA,首先可以查询需要更新的记录实体,并修改该实体的属性。 Then invoke the EntityManager to update this entity. 然后调用EntityManager来更新此实体。 thus you just update one record which you want to update. 因此,您只需更新一个要更新的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM