Search code examples
javaejb

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. 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?


Solution

  • 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. Maybe you should split that into several entities.