1. Why does the getOne(id) method fail?
See this section in the docs. You overriding the already in place transaction might be causing the issue. However, without more info this one is difficult to answer.
2. When I should use the getOne(id) method?
Without digging into the internals of Spring Data JPA, the difference seems to be in the mechanism used to retrieve the entity.
If you look at the JavaDoc for getOne(ID)
under See Also:
See Also:
EntityManager.getReference(Class, Object)
it seems that this method just delegates to the JPA entity manager's implementation.
However, the docs for findOne(ID)
do not mention this.
The clue is also in the names of the repositories.
JpaRepository
is JPA specific and therefore can delegate calls to the entity manager if so needed.
CrudRepository
is agnostic of the persistence technology used. Look here. It's used as a marker interface for multiple persistence technologies like JPA, Neo4J etc.
So there's not really a 'difference' in the two methods for your use cases, it's just that findOne(ID)
is more generic than the more specialised getOne(ID)
. Which one you use is up to you and your project but I would personally stick to the findOne(ID)
as it makes your code less implementation specific and opens the doors to move to things like MongoDB etc. in the future without too much refactoring :)