Updated July 6, 2023
Introduction to Spring Hibernate Integration
As we have looked at other articles regarding Spring and Hibernate, Spring is an excellent Java EE framework, and Hibernate is the most popular ORM framework. Both of them are widely used in the industry. Now, the Java EE framework is a powerful tool used in the entire industry to create broad bodies of enterprise applications at various levels of complexity. With new advancements, Java has brought Spring, widely used for this purpose. On the other hand, ORM frameworks are object-oriented programming that intends to wrap around a relational database virtually. Now that we have a fair idea of what both are, let us look at the integration of both.
What is Spring Hibernate Integration?
In this article, we will talk about the integration of Spring and Hibernate, and for the sake of uniformity, we will take Spring 4 and Hibernate 3 as examples. One might encounter some compatibility issues while running their code, as all version of Spring is only compatible with some versions of Hibernate if you happen to get an error saying “java.lang.NoClassDefFoundError,” which means that the particular versions of Spring and Hibernate are not compatible with each other. Now one must wonder why these two functionalities might not be suitable for use.
The error is mainly caused by moving a function from one class to another by virtue of modularity. So, it is not that the functionality is reduced but preparing itself for futuristic stability. Also, in this article, we would keep the coding portion as little as possible and make you better understand the intuition behind the principle.
Why do we use Spring Hibernate Integration?
In the Spring Framework, we have an availability of the HibernateTemplate class. This class, in particular, helps the developer to combine the usability of the creation of configuration, BuildSessionFactory, creation of sessions, beginning a transaction, committing the transaction, and much more! So, it saves a lot of coding. Let us look at this advantage with the help of an example.
If we want to use Hibernate without Spring, we will write the code as follows:
Code:
//creating configuration
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
//creating seession factory object
SessionFactory fac=cfg.buildSessionFactory();
//creating session object
Session ses=fac.openSession();
//creating transaction object
Transaction txn=ses.beginTransaction();
Student s1=new Student(1234,"Ajay", "Class-11");
session.persist(s1);//persisting the object
txn.commit();//transaction is commited
session.close(); //Closing the session
To combine the above lines of code into one line using the HibernateTemplate present in Spring, we would write:
Student s1=new Student(1234,"Ajay", "Class-11");
hibernateTemplate.save(s1);
So, by just writing two lines of code, we can eliminate 9-10 lines of code, and the code looks cleaner. Not only the above advantage, but we also have advantages like HibernateDaoSupport, which supports DAO implementation and also extends the advantage of HibernateTemplate class. If a particular application is not using HibernateTemplate, then it is better to use HibernateDaoSupport. Last but not least, Spring also provides MVC integration wherein one can use OpenSessionInViewFilter or OpenSessionInViewInterceptor for creating sessions per request instead of creating sessions per thread.
Application of Spring Hibernate Integration
1. When we develop the integration between Spring and Hibernate, we must first build the project structure.
The project structure includes:
- DAO class
- Main class
- Entity Bean
- Spring Bean
- Config files
- Maven dependencies
- Database setup script
2. First, we would look into the maven dependencies. The pom.xml file incorporates the Maven dependencies, where the developer must declare all the required dependencies for a project along with their specific versions. In pom.xml, we need to make sure that we keep some essential dependencies like spring-context and spring-tx, which would take care of core spring functionalities. We would also keep spring-orm for ORM support by Spring. Since there is integration with Hibernate, we must keep the hibernate-entity manager and hibernate-core dependencies to take care of that area. Also, remember MySQL-connector-java for any MySQL database connection.
3. We would look into the Entity Bean. The database stores and maintains persistent data, utilizing this component. Since we have Hibernate in the flow, we would use JPA annotations for mapping, as Hibernate does support JPA implementation. The other option we have is Hibernate XML-based mapping.
4. DAO stands for Data Access Object. This component helps in providing an abstract interface to a database. The abstract interface allows for delivering the required specific data operations without exposing the details of the database. In this class, we can utilize annotation provided by Spring for easier coding!
5. we have come down to the Spring Bean configuration file. In this, we need to make sure that we specify a way to provide a database connection to Hibernate. One way is to declare everything in hibernate properties. And the other way is to create a DataSource and then pass that into Hibernate.
6. Now that we have all the required functionalities ready for integration, we would define the Main class wherein all the business problem-solving logic would fit. But make sure to set up logging properly; otherwise, one will encounter a lot of logs related to Hibernate in the log file.
Conclusion
In the above article, we have used Spring 3 as an example of uniformity. Regarding the latest trend, we have Spring 4 used widely in the industry. The entire logic remains the same for integration except for using different classes like org.springframework.orm.hibernate4.LocalSessionFactoryBean for Bean creation or mapping of a spring4.xml in the Main class. Migration from Hibernate 3 to 4 is as smooth as butter!
Recommended Articles
This is a guide to the Spring Hibernate Integration. Here we discuss the basic concept, applications, and why we use Spring Hibernate Integration. You may also have a look at the following articles to learn more –