Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA - Why are changes to a returned Entity automatically persisted?

I present the question with an example.

Assert that we have a Repository such as the below:

public interface ExampleObjectRepository extends CrudRepository<ExampleObject, Long> {  } 

By extending the JpaRepository interface, the ExampleObject repository inherits the following method:

T findOne(ID id); 

Now, I have observed that, if I receive a reference to an ExampleObject after a call to this method, any manipulations that I make to this method are automatically saved to the database, for example:

ExampleObject pointInCase = exampleObjectRepository.findOne(1L); pointInCase.setName("Something else"); 

Reading around the subject, I understand this this signfies that ExampleObject instance is not detached.

This goes against my expectations. I would have expected that I would need to use the save method inherited from CrudRepository in order to save the changes:

T save(T entity); 

Would anyone be kind enough to confirm that objects returned from a Spring Data JPA Repository remain attached as standard, and explain how to use the API to mark a method in the repository such that it only returns detached references?

I imagine that changing the entity's state may also change its definition when it is used with said save(T entity) method, so I would also appreciate an understanding of how identity for updates are handled.

like image 267
8bitjunkie Avatar asked Nov 27 '12 16:11

8bitjunkie


People also ask

Which is provides some JPA related methods such as flushing the persistence context and delete record in a batch etc?

JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.

What is Spring persistence?

Persistence in Spring in normally done through a DAO (Data Access Object) layer. The Spring DAO layer is meant to encapsulate the persistence mechanism, so the same application data access API would be given no matter if JDBC, JPA or a native API were used.

Why do we use JpaRepository?

JpaRepository is a JPA (Java Persistence API) specific extension of Repository. It contains the full API of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and also API for pagination and sorting.

Why do we use JPA Repository in Spring boot?

JPA Repository is mainly used for managing the data in a Spring Boot Application. We all know that Spring is considered to be a very famous framework of Java. We mainly use this Spring Boot to create the Spring-based stand-alone and production-based applications with a very minimal amount of effort.


2 Answers

That's a fundamental principle of JPA. You work with attached (managed) entities, and every modification made on these managed entities is automatically made persistent.

If you don't want your changes to be persistent, then don't make changes, or rollback the transaction.

Working on detached entities would be a nightmare, because it would prevent lazy-loading all the associations. You can always call EntityManager.detach() on your entities, but I really wouldn't do that. Just try to understand how it works and deal with it. There are much more benefits than disadvantages. One of them being that you don't even have to think about saving all the changes that a complex business logic might do, since it's all done for you by JPA, transparently.

like image 171
JB Nizet Avatar answered Oct 03 '22 00:10

JB Nizet


You can make your Repository return detached entities if your Transaction is scoped to the repository. I.e. your transaction starts with entrance into the repository and gets closed when you code exits the repository.

If you work like this you have to take care that all necessary data gets loaded in one go, because otherwise you will get lazy initialization exceptions. But in many circumstances I consider this desirable because it gives you some control back when and what data is loaded, that otherwise slips easily through your fingers when using JPA.

For modifications I use a different Transaction scope which wraps the complete process of loading, changing (and implicitly persisting).

like image 44
Jens Schauder Avatar answered Oct 03 '22 00:10

Jens Schauder