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.

Advertisements

What is Maven? Why should I use it?

What is Maven?

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information [1].

Why should I use it?

  • Maven is a (automated) build tools. Maven can downloads and installs required jar/library for your project that declared on pom.xml. No need to add library one by one.

How Maven Works?

Based on my comprehension, here is how Maven works.

How Maven works

  1. Developers declare required jar/library (for example: Spring, Servlet, Hibernate, etc.) on pom.xml.
  2. Developers using some libraries on their source codes.
  3. The source codes go to compiling and building phase.
  4. At the compiling and building phase, the IDE will call the Maven to build the Java project.
  5. Maven reads required library on pom.xml.
  6. Maven downloads required library from offline repository, or …
  7. Maven downloads required library from online repository.
    And then Maven will ‘install’ the library into targeted Java project.
  8. Compiling and building process will produce a Java apps (either .jar or .war).

How can I use add Maven into my project?

On Eclipse based IDE, simply choose new Maven project template to start.
If you are working with Spring MVC in Spring Tool Suite (STS), the project is automatically an Maven project.

Are there any other options?

Yes, definitely. You can choose Ant Builder [2] or Gradle [3] in favor of Maven. Both of them are also build tools.

References

[1] http://maven.apache.org/

[2] http://ant.apache.org/

[3] http://www.gradle.org/

First Spring MVC project using Spring Tool Suite (STS) and Maven build

Goal(s)

In this article I will show you how to create your first J2EE applications using Spring Framework and Maven build.

Info

Maven is a build tools (or automation tools) for Java project. It is similar with Ant or Gradle. Maven can automatically download the required dependencies for your project.

Requirements

  1. Spring Tool Suite (STS) for Java EE (http://spring.io/tools/sts/all).
    Please choose the file according to your OS. I prefer to download the archive version because of its portability.
  2. An internet connection.
    For the first time creating a Spring MVC project, internet connection is REQUIRED. You need to download some project templates from Spring repository. Maven also would like to download some dependencies for Spring. Make sure that you have internet connection. If you are behind a proxy, please refer to this tutorial [1].

Continue reading