Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update single field using spring data jpa

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);

like image 506
Dmitrii Borovoi Avatar asked Mar 23 '15 02:03

Dmitrii Borovoi


People also ask

How do you update data on a CrudRepository?

CrudRepository save() to Add a New Instance Notice that we never specified an id. The instance is initially created with a null value for its id, and when we call the save() method, an id is automatically generated. The save() method returns the saved entity, including the updated id field.

How do I update entity in JPA?

In this example you will learn how to update an entity object in JPA. We use the EntityManager. merge() method to update an entity. This method takes the entity to be saved as the parameter and return the merged entity back as the result.

What is @DynamicUpdate in JPA?

@DynamicUpdate is a class-level annotation that can be applied to a JPA entity. It ensures that Hibernate uses only the modified columns in the SQL statement that it generates for the update of an entity. In this article, we'll take a look at the @DynamicUpdate annotation, with the help of a Spring Data JPA example.

What is difference between JpaRepository and CrudRepository?

CrudRepository: provides CRUD functions. PagingAndSortingRepository: provides methods to do pagination and sort records. JpaRepository: provides JPA related methods such as flushing the persistence context and delete records in a batch.


1 Answers

You can try something like this on your repository interface:

@Modifying @Query("update EARAttachment ear set ear.status = ?1 where ear.id = ?2") int setStatusForEARAttachment(Integer status, Long id); 

You can also use named params, like this:

@Modifying @Query("update EARAttachment ear set ear.status = :status where ear.id = :id") int setStatusForEARAttachment(@Param("status") Integer status, @Param("id") Long id); 

The int return value is the number of rows that where updated. You may also use void return.

See more in reference documentation.

like image 88
Bruno Ribeiro Avatar answered Sep 24 '22 21:09

Bruno Ribeiro