Table of Contents [hide]
We will use following annotations for CRUD operation.
Method
|
Description
|
Get
|
It is used to read resource
|
Post
|
It is used to create new resource.
It is not idempotent method |
Put
|
It is generally used to update resource.It is idempotent method
|
Delete
|
It is used to delete resource
|
Idempotent means result of multiple successful request will not change state of resource after initial application
For example :
Delete is idempotent method because when you first time use delete, it will delete the resource (initial application) but after that, all other request will have no result because resource is already deleted.
Post is not idempotent method because when you use post to create resource , it will keep creating resource for each new request, so result of multiple successful request will not be same.
Source code:
Here are steps to create a Spring Restful web services with hibernate integration.
Maven dependencies
2) We need to add Jackson json utility in the classpath.
Now change pom.xml as follows:
pom.xml
Please change context:component-scan if you want to use different package for spring to search for controller.Please refer to spring mvc hello world example for more understanding.
dataSource bean is used to specify java data source. We need to provide driver, URL , Username and Password.
transactionManager bean is used to configure hibernate transaction manager. hibernate4AnnotatedSessionFactory bean is used to configure FactoryBean that creates a Hibernate SessionFactory. This is the common way to set up a shared Hibernate SessionFactory in a Spring application context, so you can use this SessionFactory to inject in Hibernate data access objects.
Create bean class
4) Create a bean name “Country.java” in org.arpit.java2blog.bean.
@Entity is used for making a persistent pojo class.For this java class,you will have corresponding table in database. @Column is used to map annotated attribute to corresponding column in table. So Create Country table in mysql database with following code:
Create Controller
5) Create a controller named “CountryController.java” in package org.arpit.java2blog.controller
Create DAO class
@Repository is specialised component annotation which is used to create bean at DAO layer. We have use Autowired annotation to inject hibernate SessionFactory into CountryDAO class. We have already configured hibernate SessionFactory object in Spring-Servlet.xml file.
Create Service class
It is service level class. It will call DAO layer class.
7) It ‘s time to do maven build.
Right click on project -> Run as -> Maven build


Run the application

Post method
12) Post method is used to create new resource. Here we are adding new Country India to country list, so you can see we have used new country json in post body.
URL: “http://localhost:8080/SpringRestHibernateExample/addCountry”.

Lets see corresponding entry in Country table in database.

Lets create 3 more countries i.e. china, nepal and USA in similar way.
Put Method
13) Put method is used to update resource. Here will update population of nepal using put method.
We will update country json in body of request.
URL : “http://localhost:8080/SpringRestHibernateExample/updateCountry”

Lets check Nepal’s population in database.

Delete method
14) Delete method is used to delete resource.We will pass id of country which needs to be deleted as PathParam. We are going delete id:3 i.e. Nepal to demonstrate delete method.
URL : “http://localhost:8080/SpringRestHibernateExample/deleteCountry/3”

Lets check entry in database now.

As you can see, we have deleted country with id 3 i.e. Nepal
Project structure:

We are done with Spring Restful web services json CRUD example. If you are still facing any issue, please comment.
If you getting 404 error with above steps, you may need to follow below steps:
1) If you are getting this warning into your Tomcat startup console log, then it can cause the issue

- Go to server view
- Double click on your tomcat server. It will open the server configuration.
- Under server options check ‘Publish module contents to separate XML files’ checkbox.
- Restart your server. This time your page will come without any issues.


This should solve you issues.
Enti
i have made database in mysql and made hiberate.cfg but how to access the data that are in database using this project
after downloading where should we change
Hi Rohit,
You need to configure mysql and hibernate. I think you have already done it.
You also need to create a table in database as below and run the application. It should work. Let me know if you are still facing any issues.
CREATE TABLE COUNTRY
(
id int PRIMARY KEY NOT NULL AUTO_INCREMENT,
countryName varchar(100) NOT NULL,
population int NOT NULL
)
;
I have implemented the example, I am able to fetch data from database in json format, but other methods like getById, addDetails, Update, delete are not working. Status 500. Please help me to resolve this issue
Thank you