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?