Introduction to Spring Hibernate Integration
As we have had a look at other articles regarding Spring and Hibernate that Spring is an excellent Java EE framework and Hibernate is the most popular ORM framework and both of them are widely used in the industry. Now, Java EE framework is a powerful tool which is used in the entire industry to create broad bodies of enterprise application in various levels of complexity. With new advancements Java has brought in Spring is the one which is widely used for this purpose. On the other hand, ORM frameworks are object-oriented programming which is used with an intent to virtually wrap around a relational database. Now that we have a fair idea on what both are let us look at the integration of both.
What is Spring Hibernate Integration?
In this article, we would talk about the integration of Spring and Hibernate and for the sake of uniformity, we would take Spring 4 and Hibernate 3 for example purposes. There are some compatibility issues one might run into while running their code as all version of Spring is not compatible with all 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 be wondering that these 2 functionalities might not be very suitable for use. Let us answer that for you, the error mainly is caused by moving of a function from one class to another by virtue of modularity. So, it is not that the functionality is reduced, but preparing itself for the futuristic stability. Also, in this article, we would try to keep the coding portion as less 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 creation of configuration, BuildSessionFactory, creation of sessions, begin a transaction, committing of the transaction and a lot more! So, in a nutshell, 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 would 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 just write:
Student s1=new Student(1234,"Ajay", "Class-11");
hibernateTemplate.save(s1);
So, we can see that just writing 2 lines of code we are able to get rid of 9-10 lines of code and also 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 the 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 need to 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 maven dependencies are a part of pom.xml where all the dependencies required in a project need to be declared there along with specific versions required by the developer. 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. Now since there is integration with Hibernate, we need to keep hibernate-entity manager and hibernate-core dependencies to take care of that area. Also, not to forget about mysql-connector-java for any MySQL database connection.
3. we would look into the Entity Bean. This component is used for representing persisted data which is maintained in the database. 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 helps in providing the required specific data operations without exposing the details of the database. In this particular class, we can utilize annotation provided by Spring for easier coding!
5. Now what we have come down to is 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 into. But make sure to set up logging properly otherwise one would encounter a lot of logs related to Hibernate in the log file.
Conclusion
In the above article, for the sake of uniformity, we have used Spring 3 as an example. As far as the latest trend we have Spring 4 used widely in the industry. The entire logic remains the same for integration except for usage of some 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 do we use Spring Hibernate Integration? You may also have a look at the following articles to learn more –
4 Online Courses | 6 Hands-on Project | 38+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses