How do I update an entity using spring-data-jpa?
Identity of entities is defined by their primary keys. Since firstname
and lastname
are not parts of the primary key, you cannot tell JPA to treat User
s with the same firstname
s and lastname
s as equal if they have different userId
s.
So, if you want to update a User
identified by its firstname
and lastname
, you need to find that User
by a query, and then change appropriate fields of the object your found. These changes will be flushed to the database automatically at the end of transaction, so that you don't need to do anything to save these changes explicitly.
EDIT:
Perhaps I should elaborate on overall semantics of JPA. There are two main approaches to design of persistence APIs:
insert/update approach. When you need to modify the database you should call methods of persistence API explicitly: you call
insert
to insert an object, orupdate
to save new state of the object to the database.Unit of Work approach. In this case you have a set of objects managed by persistence library. All changes you make to these objects will be flushed to the database automatically at the end of Unit of Work (i.e. at the end of the current transaction in typical case). When you need to insert new record to the database, you make the corresponding object managed. Managed objects are identified by their primary keys, so that if you make an object with predefined primary key managed, it will be associated with the database record of the same id, and state of this object will be propagated to that record automatically.
JPA follows the latter approach. save()
in Spring Data JPA is backed by merge()
in plain JPA, therefore it makes your entity managed as described above. It means that calling save()
on an object with predefined id will update the corresponding database record rather than insert a new one, and also explains why save()
is not called create()
.
Since the answer by @axtavt focuses on JPA
not spring-data-jpa
To update an entity by querying then saving is not efficient because it requires two queries and possibly the query can be quite expensive since it may join other tables and load any collections that have fetchType=FetchType.EAGER
Spring-data-jpa
supports update operation.
You have to define the method in Repository interface.and annotated it with @Query
and @Modifying
.
@Modifying
@Query("update User u set u.firstname = ?1, u.lastname = ?2 where u.id = ?3")
void setUserInfoById(String firstname, String lastname, Integer userId);
@Query
is for defining custom query and @Modifying
is for telling spring-data-jpa
that this query is an update operation and it requires executeUpdate()
not executeQuery()
.
You can specify other return types:int
- the number of records being updated.boolean
- true if there is a record being updated. Otherwise, false.
Note: Run this code in a Transaction.
You can simply use this function with save() JPAfunction, but the object sent as parameter must contain an existing id in the database otherwise it will not work, because save() when we send an object without id, it adds directly a row in database, but if we send an object with an existing id, it changes the columns already found in the database.
public void updateUser(Userinfos u) {
User userFromDb = userRepository.findById(u.getid());
// crush the variables of the object found
userFromDb.setFirstname("john");
userFromDb.setLastname("dew");
userFromDb.setAge(16);
userRepository.save(userFromDb);
}
As what has already mentioned by others, the save()
itself contains both create and update operation.
I just want to add supplement about what behind the save()
method.
Firstly, let's see the extend/implement hierarchy of the CrudRepository<T,ID>
,
Ok, let's check the save()
implementation at SimpleJpaRepository<T, ID>
,
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
As you can see, it will check whether the ID is existed or not firstly, if the entity is already there, only update will happen by merge(entity)
method and if else, a new record is inserted by persist(entity)
method.