SpringMVC Hibernate CRUD Tutorial using Eclipse

I am going to create today a complete End-to-End SpringMVC 3, Hibernate 3 CRUD tutorial which uses MySQL as a database to store records. This will be a simple Bookstore application where user can Create, Update, View of Delete book records. We will use Hibernate 3 as an ORM tool with Spring MVC as a MVC Framework.

In this tutorial you will learn how we integrate SpringMVC with Hibernate using JPA annotations and I will also going to use Spring annotation configuration for Controller, Service and Repository annotations. The main advantage of using @Repository or @Service over @Component is that it’s easy to write an AOP pointcut that targets, for instance, all classes annotated with @Repository.

Upon completion of this tutorial the Application will look like following screenshot.

Lets Start

First of all, We need a SQL Table to save our Books details in RDBMS. You can create this table by the help of Hibernate also but its good to follow Bottom-Up approach to create an Application. Following SQL used to create a Books table in MySQL database.

Hibernate Entity Class: Book

The @Entity annotation is used to mark this class as an Entity bean. So the class should atleast have a package scope no-argument constructor.

The @Table annotation is used to specify the table to persist the data. The name attribute refers to the table name. If @Table annotation is not specified then Hibernate will by default use the class name as the table name.

The @Id annotation is used to specify the identifier property of the entity bean. The placement of the @Id annotation determines the default access strategy that Hibernate will use for the mapping. If the @Id annotation is placed over the field, then filed access will be used. Instead if it placed over the getter method of that field, then property access will be used. Here we use property access.

The @GeneratedValue annotation is used to specify the primary key generation strategy to use. If the strategy is not specified by default AUTO will be used.

Data Access Layer

Data access layer of our application consist of one BookDoa Interface and its implementation BookDaoImpl class. The BookDaoImpl class has a @Repository annotation which used to enable this class to eligible for persistence exception translation.


Service Layer

Service layer also consist of one Service interface BookService and its implementation class BookServiceImpl.java. We have annotated BookServiceImpl class with @Service annotation.

BookServiceImpl.java class…

Book Controller

Now create a BookController which will have all the method we need for our CRUD operations. We need to wire the bookService for this controller using @Autowired annotation.

SpringMVC Configuration

Now we are all set with our java changes and we need to add spring relate configuration in xml files. First we add DispatcherServlet reference in web.xml file. Following xml tags will add a DispatcherServlet reference to web.xml file.

Now create a jdbc.properties file which is used to set Database related properties into it. We can add those properties directly into spring xml file but it’s good to create a separate file and have all details of DB into it.

Hibernate Configuration

We have created a Hinerbate Entity Book which is annotation base Entity. The benefit of creating an annotation based Entity class is We don’t need to create a .hbm.xml file for that entity but we need to create hibernate.cfg.xml file for Hibernate related configuration. Following XML shows the mininum Hibernate configuration File.

Spring Configuration

Now we need to add spring-servler.xml file which contain all beans and others MVC, Transaction related Tags. XML file is minimal and self-explanatory.

View creation

Now last but not the least, creating view to show our Book Add Form. I have also created a Table grid which show the List of all available Books after Book Entry form. You can click on Book Name and it will show the details for the Book. Further you can edit the details and Save it to Update the Changes.


The Output will Look similar to….

Download Source

About the Author

+ has written 38 posts.

I Love Coding, Analyzing, Designing and doing it the right way. I am obsessed about learn anything new daily and sharing what I have learn each Day. This Blog is journey of my Zeal, going through step by step to the mountain of desire to become prefect. Google

Copyright © 2014 TechZoo - Technology Blog. All rights reserved.
Proudly powered by WordPress. Developed by 7Tech Solutions