Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update or SaveorUpdate in CRUDRespository, Is there any options available

I am trying to do CRUD operations with My Entity bean. CRUDRepository provide standard methods to find, delete and save but there is no generic method available like saveOrUpdate(Entity entity) that in turn calls Hibernate or HibernateTemplate sessions saveorUpdate() methods.

The way CRUDRepository provides this functionality is to use like this

@Modifying @Query("UPDATE Space c SET c.owner = :name WHERE c.id = :id") Integer setNameForId(@Param("name") String name, @Param("id") 

but this is not generic and needs to be written for complete form fields. Please let me know if there is any way or i can get session of Hibernate or object of Spring HibernateTemplate to solve this issue.

like image 399
Kul Bhushan Prasad Avatar asked Jun 26 '14 00:06

Kul Bhushan Prasad


People also ask

How do I update my crud repository?

CrudRepository save() to Update an Instance We can use the same save() method to update an existing entry in our database. Suppose we saved a MerchandiseEntity instance with a specific title: MerchandiseEntity pants = new MerchandiseEntity( "Pair of Pants", 34.99); pants = repo. save(pants);

Which method is used for update in JPA repository?

Hibernate's @DynamicUpdate annotation, which dynamically rewrites the update query. JPA's @Column annotation, as we can disallow updates on specific columns using the updatable parameter.

How does hibernate saveOrUpdate work?

saveOrUpdate() Hibernate will check if the object is transient (it has no identifier property) and if so it will make it persistent by generating it the identifier and assigning it to session. If the object has an identifier already it will perform . update() .

Does Spring JPA close connection?

The application displays the result to the end-user. The application closes the connection, which returns the connection to the pool. Note: The application calls the close() method, which allows the connection to remain open.

What is the use of save () method in crudrepository?

The save () method returns the saved entity, including the updated id field. 5. CrudRepository save () to Update an Instance We can use the same save () method to update an existing entry in our database. Suppose we saved a MerchandiseEntity instance with a specific title:

How do I update a crudrepository instance?

CrudRepository save () to Update an Instance We can use the same save () method to update an existing entry in our database. Suppose we saved a MerchandiseEntity instance with a specific title: Later, we found that we wanted to update the price of the item.

What is the use of update () and saveorupdate () method of session class?

update () method runs the update query against the identifier. saveOrUpdate () method of Session class works as save () or update () method. First hibernate checks the existence of instance of entity and if not available then inserts the data into database and if available then updates the data.

What is crudrepository in spring?

CrudRepository is a Spring Data interface for generic CRUD operations on a repository of a specific type. It provides several methods out of the box for interacting with a database. In this tutorial, we'll explain how and when to use the CrudRepository save() method.


1 Answers

The implementation of the method

<S extends T> S save(S entity)

from interface

CrudRepository<T, ID extends Serializable> extends Repository<T, ID>

automatically does what you want. If the entity is new it will call persist on the entity manager, otherwise it will call merge

The code looks like this:

public <S extends T> S save(S entity) {      if (entityInformation.isNew(entity)) {         em.persist(entity);         return entity;     } else {         return em.merge(entity);     } } 

and can be found here. Note that SimpleJpaRepository is the class that automatically implements CrudRepository in Spring Data JPA.

Therefore, there is no need to supply a custom saveOrUpdate() method. Spring Data JPA has you covered.

like image 71
geoand Avatar answered Sep 20 '22 09:09

geoand