Spring Hibernate Integration Using HibernateDaoSupport. Spring Hibernate Integration Using HibernateDaoSupport. The sample program demonstrates Spring and. A simple Hibernate application. Struts 2 Tutorial Series: Sample Application. Spring Hibernate Integration Example Tutorial. Spring is one of the most used Java EE Framework and Hibernate is the most popular ORM framework. That’s why Spring Hibernate combination is used a lot in enterprise applications. Spring Hibernate Integration Example Tutorial. Spring 4 Hibernate 3 Test Program. Hibernate Spring; Hibernate Spring MVC; Hibernate Struts 2. Hibernate tutorial with full example, including Hibernate configuration. Example to integrate Hibernate with Struts framework. Struts + Spring + Hibernate Integration. Recently I have written a lot for Spring Tutorial and Hibernate Tutorial, so a post for spring hibernate integration was due for long time. Today in this tutorial, we will use Spring 4 and integrate it with Hibernate 3 and then update the same project to use Hibernate 4. Since there are a lot of versions for Spring and Hibernate both and Spring ORM artifact supports both Hibernate 3 and Hibernate 4, it’s good that I list all the dependencies I have used in my project. Note that I have noticed that all spring and hibernate versions are not compatible, below versions have worked for me so I think they are compatible. If you are using some other versions and getting java. No. Class. Def. Found. Spring Hibernate Integration Using Java Persistence API. Sample Program Overview The sample program demonstrates the Spring. Error, then it means that they are not compatible. Mostly it’s because Hibernate classes are moved from one package to another causing this error. For example org. hibernate. Filter. Definition class is moved to org. Filter. Definition in latest hibernate versions. Spring Framework Version: 4. RELEASEHibernate Core and Hibernate Entity. Manager Version: 3. Final and 4. 3. 5. Final. Spring ORM Version: 4. RELEASEDatabase Setup. I am using My. SQL database for my project, so below setup. Notice that I am using version 4. RELEASE. spring- orm dependency for Spring ORM support, it’s required for hibernate integration in our spring project. Hibernate framework. Notice that version is 3. Final, for using Hibernate 4 all we need is to change it to 4. Final as commented in above pom. My. SQL driver for database connection. Model Class or Entity Bean. We can use Hibernate XML based mapping as well as JPA annotation based mapping. Here I am using JPA annotations for mapping because hibernate provides JPA implementation. This pattern makes our implementation flexible and easy to migrate from one technology to another. For example, if we want to use i. Batis ORM framework, all we need is to provide a DAO implementation for i. Batis and then change the spring bean configuration file. In above example, I am using Hibernate session transaction management. But we can also use Spring declarative transaction management using @Transactional annotation, read more at Spring Transaction Management. Spring Bean Configuration File for Hibernate 3 Integration. Let’s first look at the spring bean configurations we need for Hibernate 3 integration, we will look into detail later on. I prefer the second approach, that’s why we have Apache Commons DBCP dependency to create a Basic. Data. Source by setting database connection properties. For Spring and Hibernate 3 integration, Spring ORM provides two classes – org. Local. Session. Factory. Bean when hibernate mappings are XML based and org. Annotation. Session. Factory. Bean for annotations based mapping. I have provided simple bean configuration of Local. Session. Factory. Bean in comments, if you are using XML based mappings. Annotation. Session. Factory. Bean extends Local. Session. Factory. Bean class, so it has all the basic properties for hibernate integration. The properties are self understood and mostly hibernate related, so I will not go into much detail for them. But if you are wondering from where hibernate. Properties, annotated. Classes are coming, you need to look into the bean class source code. Notice the bean definition of person. DAO, like I said earlier if we have to switch to some other ORM framework, we need to change the implementation class here and set any other properties we need. Spring 4 Hibernate 3 Test Program. Our setup is ready now, let’s write a simple program to test our application. However we get following output generated by our program. For this migration, we need to make only following configuration changes. Change the hibernate version to 4. Final in the pom. Change the spring bean configuration file, till now you must have figured out that Spring bean configuration file is the key for integration of spring and hibernate framework. Below spring bean configuration file will work for Spring 4 and Hibernate 4 versions. All the other configurations are same as before. That’s it, our project is successfully migrated to Hibernate 4, neat isn’t it. Just change the Spring. Hibernate. Main class to use spring. You can download the final project from below link and play around with more configurations to learn more.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |