Spring-ORM is a technique or a Design Pattern used to access a relational database from an object-oriented language. ORM (Object Relation Mapping) covers many persistence technologies. They are as follows:
JPA(Java Persistence API): It is mainly used to persist data between Java objects and relational databases. It acts as a bridge between object-oriented domain models and relational database systems.
JDO(Java Data Objects): It is one of the standard ways to access persistent data in databases, by using plain old Java objects (POJO) to represent persistent data.
Hibernate – It is a Java framework that simplifies the development of Java applications to interact with the database.
Oracle Toplink, and iBATIS: Oracle TopLink is a mapping and persistence framework for Java development.
Spring ORM with Hibernate
Spring ORM is a module of the Java Spring framework used to implement the ORM(Object Relational Mapping) Technique. It can be integrated with various mapping and persistence frameworks like Hibernate, Oracle Toplink, iBatis, etc. for database access and manipulation. This article covers an example of the integration of the Spring ORM module with Hibernate framework.
Prerequisites:
Java basics
Spring core
Hibernate or any other ORM tool
Spring ORM provides various classes and interfaces for integrating Spring applications with Hibernate framework. Some useful classes in Spring ORM are:
HibernateTemplate
HibernateTransactionManager
LocalSessionFactoryBean
HibernateTemplate is used to perform database operations. It provides various methods which facilitate the insertion, deletion, modification, and retrieval of data from the database. Useful methods of HibernateTemplate are as follows:
void clear()
void delete(Object entity)
<T> T get(Class<T> entityClass, Serializable id)
<T> T load(Class<T> entityClass, Serializable id)
<T> List<T> loadAll(Class<T> entityClass)
Serializable save(Object entity)
void saveOrUpdate(Object entity)
void update(Object entity)
HibernateTemplate requires an object of SessionFactory. LocalSessionFactoryBean is a class present in the Spring ORM module which provides the object of SessionFactory. LocalSessionFactoryBean takes the following properties:
DataSource: contains information like driverClassName, URL, username, password, etc.
HibernateProperties: used to set various hibernate properties like hibernate dialect, show SQL queries, etc.
AnnotatedClasses/MappingResources: used to provide annotated beans or mapping resources for the entities based on which hibernate constructs the tables in the database.
HibernateTransactionManager is used to handle transactional logic when the application consists of data modification operations on the database.
Comments