Spring MVC Tutorial – Hibernate Integration. After a long gap. Spring Tool Suite Hibernate Tutorial JavaThe project libraries you will have to edit to match your setup. Along with learning to integrate Hibernate, I’ll be introducing some new pieces of Spring MVC. For this installment, I am using the following: JDK 6 update 1. Tomcat 6. 0. 2. 0Spring. Source Tool Suite 2. IDEMy. SQL 5. 1. Spring Framework 2. SEC0. 1 (the bundle with all dependencies included so that I don’t have to look for jars here and there)JSTL 1. Hibernate Distribution 3. GAHibernate Annotations 3. Looking for Groovy and Grails Tool Suite? The Spring Tool Suite as well as the Groovy & Grails Tool Suite are built on top of the Eclipse platform. Simple Hibernate XML Configuration. Spring by Example Version 1.5 Spring by Example ii. SpringSource Tool Suite Setup. DateTime Manipulation 4 days ago. How Hibernate Almost Ruined My Career 18. Application with Spring Framework: A Beginner's Tutorial. Spring MVC Tutorial for Beginners using Spring Tool Suite or Eclipse. Primefaces Tutorial; JDBC Tutorial; Hibernate Tutorial; MongoDB Tutorial; #Interview Questions. SpringSource Tool Suite 2.2.0 as my IDE; MySQL 5.1; Spring Framework 2.5.6.SEC01. In this tutorial you will see how to integrate spring. I assume you are comfortable with both spring and hibernate. Spring Hibernate Integration Tutorial. GAlog. 4j 1. 2. 1. JDBC Driver – My. SQL Connector/J 5. The project is named . We’ll be doing customer registration only. Without any fuss, let’s start. Setup the Initial Project in STSStart STSCreate a Dynamic Web Project from File> New> Other. Name the project . Create one if already not available. Keep everything else default (the source, class and web content directories)After the project is created, right- click on the root node and choose Spring Tools> Add Spring Project Nature. Add the necessary libraries. Following jars will be required: JARSourcespring. Spring Frameworkjstl- api- 1. GA. jar, jta- 1. 1. Hibernate Core Distributionhibernate- annotations. Hibernate Annotationslog. Log. 4Jslf. 4j- api- 1. SLF4. Jmysql- connector- java- 5. My. SQL Connector/JHappy hunting! Configure web. xml Spring MVCRewrite the auto- generated web. Spring Core; Spring MVC. How to generate Hibernate mapping files & annotation with Hibernate Tools. Java EE 5 Tutorial; Java. Spring Tool Suite Ver 3. The Spring Tool Suite. STS supplies tools for all of the latest enterprise Java and Spring. SpringSource Tool Suite (STS) for Eclipse Helios (3.6). In this tutorial you will learn to create a complete Web application from scratch. Spring Security usage. We recommend the use of SpringSource Tool Suite. Spring MVC Tutorial): < ? To do so,Right- click Web. Content in the project explorer and then New> JSPName it index. Clean up the generated mess after the @page tag and add just one redirect line: < %@ page language=”java” content. Type=”text/html; charset=UTF- 8. After writing the controller, we have to map it in ibank- servlet. JSP that will be used as the returned view. Spring Tool Suite Hibernate Tutorial EclipseFirst, the Controller. The Index. Controller is a very simple one that records the system date and passes control to Dispatcher. Servlet with the view name . The three files created should be shown in the Config Files tab. Go to Config Sets tab and click the New button. Give a name of your choice (I chose ibank- beans), click Select All and then OKClose the Properties dialog. Mapping the Controller and View. Now we make the Spring MVC related configurations in ibank- servlet. First we setup the controller mapping using Simple. Url. Handler. Mapping (which will be used for other controllers as well) and the setup the view resolver Internal. Resource. View. Resolver which takes any view name and generates the corresponding JSP page name.< ? So, create a new folder names jsp under WEB- INF and then create a new JSP file names home. Type=”text/html; charset=UTF- 8. Then start the server by right- click and Start. Fire up your browser and point to http: //localhost: 8. Setting Up the Admin Site. I’ll be doing some fun stuff here to make the site look a bit better. Some CSS techniques are applied (not my invention). Under WEB- INF/jsp, create a new folder named admin. Create three JSP files: admin. Under Web. Content folder, create a new CSS file names master. Edit the files so that they look similar to the followingmaster. CHARSET “UTF- 8”; html, body . Instead, we’ll be using one of Spring MVC’s built- in controller as there is no processing involved from our part. Add this in ibank- servlet. Home. Page. Controller” class=”org. Parameterizable. View. Controller”> < property name=”view. Name” value=”admin/admin”/> < /bean> The admin/admin view name get mapped to /WEB- INF/jsp/admin/admin. Go, test the new admin page. Hibernate – Configuration Basics. This is not a tutorial about Hibernate. So don’t expect even a decent introduction! The starting point of a Hibernate setup is the Session. Factory. Session. Factory can be configured using an XML file or using Annotations or even raw programmatic manipulation. For the traditional XML configuration using . Session. Factory sf = new Configuration(). Session. Factory(); For the annotation- supported Session. Factory (supports both EJB3 and Hibernate- specific annotation), use the following: Session. Factory sf = new Annotation. Configuration(). configure(). Session. Factory(); In both cases, hibernate. A typical hibernate. Hibernate docs): < ? Here is the basic outline: Take out the database connection information out of Hibernate and manage it through Spring. Configure Hibernate Session. Factory using Spring’s built- in support, eliminating the need for a hibernate. Edit ibank- persistence. The session. Factory bean is our Hibernate Session. Factory configured entirely through Spring. Note that the annotated. Classes list is empty as we haven’t defined any entity classes to be persisted. We’ll add to it in due time. Defining the Persistence and DAO (Data Access Object) Classes. We want to register new customers and then approve them in our system. Create a Customer class under org. Column; import javax. Generated. Value; import javax. Generation. Type; import javax. Id; @Entitypublic class Customer . But still some discussion. We must tell Hibernate that we want a class/POJO to be treated as persistent – using the @Entity annotation. Each field to be mapped to the database needs to be marked with the @Column annotation except the primary key which is annotated with @Id. The additional @Column annotation for the id field is used to explicitly map it to a column name of our choice (CUST. The unique attribute for user. Id is set to true so that a unique index for it is used. The @Generated. Value annotation tells the persistence provider to choose the best primary key generation technique for the underlying database – an auto- increment integer for My. SQL. Instead of creating a DAO class directly, we use an interface for segregation of specification and implementation. This way we will be able to change the implemenation to JPA or some other ORM in the future – without affecting our client code. Following is the interface Customer. Dao: package org. List; import org. Customer; public interface Customer. Dao . We’re extending our DAO from Hibernate. Dao. Support which encapsulates all the gory details of dealing with Hibernate directly. The magic comes through the use of a Hibernate. Template which we get a reference to using get. Hibernate. Template(). As you can see, Hibernate. Template gives us convenient methods to do all the common things of Hibernate. Still we need to do some specifics: find. By. User. Id() uses an HQL (Hibernate Query Language) statement to filter results by user. Id and not the primary key (id in this case). All() method uses the simplest select all statement. All(start. Page, page. Size) uses Detached. Criteria (a Spring gift) for paging support. By. Example(. The data source and session factory are already there. We add our domain class Customer to the list of annotated classes. And we configure our DAO as well.< bean id=”session. Factory”. We need a service class for interacting with the UI as well. As usual we separate the interface and implementation. First, the interface: package org. List; import org. Customer; public interface Customer. Registration. Service . Now we’ll write our Spring MVC things for registering a new customer. Here’s the outline: User clicks on new customer registration link. System presents new customer input page. User fills in the details and submits. System verifies data – omitted from this tutorial. System saves the new customer and displays summary. We will first write the controller. New. Customer. Controller. Hash. Map; import java. Map; import org. himu. Customer; import org. Customer. Registration. Service; import org. Model. And. View; import org. Simple. Form. Controller; public class New. Customer. Controller extends Simple. Form. Controller . We’ve already defined a nice looking (!) template in admin. If we had used Tiles then that would have been taken care of. But we’re not using Tiles. So, for now, lets make a copy of the admin. STS by selecting it in the Project Explorer, then pressing Ctrl+C and Ctrl+V in sequence. STS understands and prompts you to provide a name for the new copy. Name it newcustomer. Following is the bean entry for New. Customer. Controller: < bean id=”new. Customer. Controller” class=”org. New. Customer. Controller”> < property name=”cust. Reg. Service” ref=”customer. Reg. Service”/> < property name=”form. View” value=”admin/newcustomer”/> < property name=”success. View” value=”admin/newcustomer- success”/> < property name=”command. Name” value=”customer”/> < property name=”command. Class” value=”org. Customer”/> < /bean> We also must not forget the url mapping under simple. Url. Mapping: < prop key=”/createcust. Customer. Controller< /prop> The Project Structure. We’re done for our first Hibernate test. Following is a snapshot of the STS project structure: That’s It! I feel this entry is quite long. But I couldn’t help but detail all the steps that I’ve gone through so that anyone can recreate the whole thing from scratch. I’ll be expanding the project to contain a Customer browsing and authorization part. Another blog entry for that. Hope this was useful to you. Comments welcome!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |