티스토리 뷰

Spring Data JPA를 적용해서 코딩하던 중 entity id로 하나의 object를 리턴 받고 싶은데 사용할 수 있는 메소드가 2가지라 찾아보았다.
http://stackoverflow.com/questions/24482117/when-use-getone-and-findone-methods-spring-data-jpa

  1. JpaRepository.getOne(id);
  2. CrudRepository.findOne(id);

According with the getOne API: Returns a reference to the entity with the given identifier.
According with the findOne API: Retrieves an entity by its id.

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 :)

둘의 차이점은 없으며 단지, getOne보다 findOne이 더 generic한 메소드이기 때문에 향후 구현 변경을 생각하면
findOne을 사용하는 것이 리팩토링량을 줄일 수 있을 것이다.


test case를 돌려본 결과
존재하지 않는 키에 대해서 findOne은 null을 반환하고, getOne은 Exception을 발생시킴

댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함