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.