CRUD with Spring MVC & Hibernate

Goals

  1. Understanding ORM (Object Relational Mapping) concept.
  2. Connecting Spring MVC applications with Hibernate.
  3. Create CRUD (create, read, update, delete) operations.

Requirement

  • Spring MVC project
  • MySQL server for database
  • Internet connections

This tutorial consists of quite long steps. Make sure that you follow every step carefully.

1. Define your database

In this example, I will use 2 tables shown below.

Snap 2013-12-31 at 09.51.57

There is one-to-many relationship between two tables (Category to Product).

Here is the SQL for creating tables above

CREATE TABLE <code>category</code> (
  <code>categoryId</code> int(11) NOT NULL AUTO_INCREMENT,
  <code>categoryName</code> varchar(45) DEFAULT NULL,
  <code>categoryDescription</code> text,
  PRIMARY KEY (<code>categoryId</code>)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

CREATE TABLE <code>product</code> (
  <code>productId</code> int(11) NOT NULL AUTO_INCREMENT,
  <code>productName</code> varchar(45) NOT NULL,
  <code>productStock</code> int(11) NOT NULL,
  <code>productPrice</code> float NOT NULL,
  <code>productDescription</code> text NOT NULL,
  <code>categoryId</code> int(11) NOT NULL,
  PRIMARY KEY (<code>productId</code>,<code>categoryId</code>),
  KEY <code>fk_Product_Category_idx</code> (<code>categoryId</code>),
  CONSTRAINT <code>fk_Product_Category</code> FOREIGN KEY (<code>categoryId</code>) REFERENCES <code>category</code> (<code>categoryId</code>) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

If you are already have database, you can also try with your own tables.

3 thoughts on “CRUD with Spring MVC & Hibernate

Leave a reply to jaka Cancel reply