ORM DAO를 만들기 위해 Spring Framework를 사용했을 때의 이점
- Easier Testing : 스프링 IoC 방법으로 인해 구현체와 configuration을 쉽게 바꿀수 있어서 조각 조각의 persistence-related code를 독립적으로 쉽게 테스트할 수 있음
- Common data access exceptions : 스프링은 ORM tool에서 발생한 예외를 감싸서 checked exception을
DataAccessException
unchecked exception의 hierarchy로 변환해줌. 그럼으로 대부분의 영속성 예외를 관리할 수 있음
- General Resource management : 스프링 어플리케이션 컨텍스트는 Hibernate의
SessionFactory
객체와 JPAEntityManagerFactory
객체,DataSource
객체 등 다른 관련된 리소스들의 location 과 configuration을 쉽게 바꿀수 있음
- Integrated transaction management : @Transactional annotation을 통해서 ORM code의 transaction을 관리할 수 있음(rollback 등등의 작업들 알아서 해줌)
General ORM Integration Considerations
- Spring의 ORM 통합에서 가장 중요한 목적 : application object들의 재사용성을 높이고 container 의존성에서 분리하면서 단순하고 일관적이도록 wiring하는 것
- 뚜렷한 application layering
- application의 object사이의 약한 결합도 — no more hard-to-replace singletons, no more custom service registries
Resource and Transaction Management
- 전형적인 비즈니스 어플리케이션은 반복적인 resource 관리 코드로 뒤범벅 되어 있음
- 이에 반해 스프링은 resource handling을 위해 간단한 해결책을 제시함
- 예를들어 Jdbc Template에서 내부적으로 connection 관리
- ORM technology에 AOP interceptor 적용
Exception Translation
- Hibernate 나 JPA를 DAO로 사용하기 위해서는 해당 기술의 native exception을 어떻게 다룰지를 결정해야 함
- 어떤 것을 사용하느냐에 따라
HibernateException
을 던질수도,PersistenceException
을 던질 수도 있음
- 그러나 호출하는 클라이언트에서 해당 구현체를 모르는 이상, 특정한 exception을 잡는 것은 불가능함
- Spring에서는
@Repository
어노테이션을 이용하여 exception translation이 적용되도록 함