Introduction to ORM with Spring
mainSpring Framework provides integration for Object-Relational Mapping (ORM) tools, specifically supporting the Java Persistence API (JPA) and native Hibernate. It manages resources, Data Access Object (DAO) implementations, and transaction strategies through Spring's Inversion of Control (IoC) container.
Key Integration Features:
- Dependency Injection: All supported ORM features can be configured via Dependency Injection, allowing them to participate in Spring's resource and transaction management.
- Exception Translation: Spring wraps proprietary ORM exceptions (which may be checked) into a consistent, unchecked
DataAccessExceptionhierarchy. This applies to both ORM tools and JDBC, allowing for a unified programming model. - Resource Management: Spring manages the lifecycle and configuration of
SessionFactory(Hibernate),EntityManagerFactory(JPA), andDataSource(JDBC) instances. It can transparently bind a HibernateSessionto the current thread. - Declarative Transaction Management: You can manage transactions using an Aspect-Oriented Programming (AOP) style, typically via the
@Transactionalannotation or XML configuration. This allows you to swap between local and JTA transaction managers without changing your ORM code.
Recommended Integration Style: Code your DAOs against plain Hibernate or JPA APIs. This ensures your code remains decoupled from Spring-specific logic while still benefiting from Spring's infrastructure enhancements.