I'm using spring-data's repositories - very convenient thing but I faced an issue. I easily can update whole entity but I believe it's pointless when I need to update only a single field:
@Entity
@Table(schema = "processors", name = "ear_attachment")
public class EARAttachment {
private Long id;
private String originalName;
private String uniqueName;//yyyy-mm-dd-GUID-originalName
private long size;
private EARAttachmentStatus status;
to update I just call method save. In log I see the followwing:
batching 1 statements: 1: update processors.ear_attachment set message_id=100,
original_name='40022530424.dat',
size=506,
status=2,
unique_name='2014-12-16-8cf74a74-e7f3-40d8-a1fb-393c2a806847-40022530424.dat'
where id=1
I would like to see some thing like this:
batching 1 statements: 1: update processors.ear_attachment set status=2 where id=1
Spring's repositories have a lot of facilities to select something using name conventions, maybe there is something similar for update like updateForStatus(int status);
Hibernate offers the @DynamicUpdate annotation. All we need to do is to add this annotation at the entity level:
Now, when you use
EARAttachment.setStatus(value)
and executing "CrudRepository"save(S entity)
, it will update only the particular field. e.g. the following UPDATE statement is executed:You can try something like this:
You can also use named params, like this:
The int return value is the number of rows that where updated. You may also use
void
return.See more in reference documentation.