Here is the code:
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {}
JpaRepository from Spring Data JPA project.
Here is the testing code:
public class JpaAccountRepositoryTest extends JpaRepositoryTest {
@Inject
private AccountRepository accountRepository;
@Inject
private Account account;
@Test
@Transactional
public void createAccount() {
Account returnedAccount = accountRepository.save(account);
System.out.printf("account ID is %d and for returned account ID is %d\n", account.getId(), returnedAccount.getId());
}
}
Here is the result:
account ID is 0 and for returned account ID is 1
Here is from CrudReporsitory.save() javadoc:
Saves a given entity. Use the returned instance for further operations as the save operation might have changed the entity instance completely.
Here is the actual code for SimpleJpaRepository from Spring Data JPA:
@Transactional
public T save(T entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
So, the question is why do we need to use the returned instance instead of the original one? (yes, we must do it, otherwise we continue to work with detached instance, but why)
The original EntityManager.persist() method returns void, so our instance is attached to the persistence context. Does some proxy magic happens while passing account to save to repository? Is it the architecture limitation of Spring Data JPA project?
The save(…)
method of the CrudRepository
interface is supposed to abstract simply storing an entity no matter what state it is in. Thus it must not expose the actual store specific implementation, even if (as in the JPA) case the store differentiates between new entities to be stored and existing ones to be updated. That's why the method is actually called save(…)
not create(…)
or update(…)
. We return a result from that method to actually allow the store implementation to return a completely different instance as JPA potentially does when merge(…)
gets invoked.
So generally it's more of an API decision to be lenient (permissible, tolerant) regarding the actual implementation and thus implementing the method for JPA as we do. There's no additional proxy messaging done to the entities passed.