Get Started Using Spring MVC and Hibernate for Your Java EE Applications
- Overview / Setting Up the Database / Creating the Web Project
- Configuring Spring-MVC
- Configuring Hibernate
- Creating the Table View, Interface, and Implementation Classes
- Creating the Table Controller / Creating the Controller View
This article shows how to use Spring MVC with a popular object relational mapping (ORM) framework called Hibernate. ORM is becoming the most popular way to manage a Java application's persistence layer. An ORM framework maps your database tables to objects in the application with the same name, making the application much easier to construct and maintain. In short, ORM decouples your business-logic layer from your database. With ORM, you use the framework's SQL on your domain model objects—not on the actual database tables. With this type of development, you can change your application's database (for example, from Oracle to SQL Server) without making code changes to your queries, saving a good amount of time.
The application is a simple registration page. Many applications have this page, so you can build and expand it into a full-blown application with all the legwork out of the way.
Setting Up the Database
To get started, choose a persistence store. Hibernate will work with almost any type of database; for this article, I'm using MySQL. I haven't used MySQL for awhile, and I must admit that I'm impressed by the improvements since I last used it.
Our first task is to create a new schema (database):
- Using the client workbench, click Create a New EER Model. You'll get the existing default model page for the default mydb schema. By creating or modifying your model first, you're using the "data first" approach to building your ORM application.
- Click the plus (+) icon on the far right of the screen to add a new model for what will become a database schema.
- Give the model a name that fits your application, and save your changes.
Now you can begin working with your new database schema. Any changes you make to the model can be synchronized with the schema by selecting Database > Query Database from the top menu bar of the editor. But we don't need to add any tables to our model; as you'll see later in this article, Hibernate will do this for us automatically.
Creating the Web Project
Now that the table and database are set up, it's time to create the Spring MVC project. I'm using Eclipse for this example, but any project management tool will work.
- Because Eclipse doesn't come with the Spring JAR files, download them from the Spring Community Downloads page and add them to your project.
- Download the Hibernate JAR files.
- Using Eclipse, create a dynamic web project.
- Once the project has been created, add the Spring JAR files (from the Spring framework's dist folder) to this directory in your project:
WebContent\WEB-INF\libAn easy way to do this is to drag-and-drop the files from the framework folder onto the web project's lib folder.
- Do the same thing for Hibernate by copying the files from the Hibernate framework's lib\required folder to the project's lib directory.
- You'll need some additional JAR files to make everything work:
commons-dbcp-1.4 commons-logging-1.1.1 commons-pool-1.6