我正在使用spring-data的repositories - 这是非常方便的东西,但是我遇到了一个问题。我可以轻松地更新整个实体,但我认为当我只需要更新单个字段时,这是毫无意义的:
@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;
我只需调用save方法来更新。在日志中,我看到以下内容:
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
我希望能看到类似这样的东西:
batching 1 statements: 1: update processors.ear_attachment set status=2 where id=1
Spring的存储库有很多使用名称约定选择某些内容的设施,也许有类似于updateForStatus(int status)的更新方法。
em.createQuery
,如果我没记错的话,所以是一样的。如果你想要类型安全,你需要知道哪些属性被修改来构建查询。之后,你可以使用自定义存储库来执行查询 (http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/#repositories.custom-implementations)。 - Bruno Ribeiro