Tutorial: Create Spring 3 MVC Hibernate 3 Example using Maven in Eclipse. For this application we will also use Maven for build and dependency management and My. SQL as database to persist the data. The application will be a simple Contact Manager app which will allow user to add new contacts.
The list of contacts will be displayed and user will be able to delete existing contacts. Our Goal. As describe above, our goal is to create a contact manager application which will allow the user to add a contact or remove it.
The basic requirement of the Contact Manager app will be: Add new contact in the contact list. Display all contacts from contact list. Delete a contact from contact list. Spring 3. 0 MVC Series. Related: Following is the screenshot of end application. Application Architecture. We will have a layered architecture for our demo application.
The database will be accessed by a Data Access layer popularly called as DAO Layer. This layer will use Hibernate API to interact with database. The DAO layer will be invoked by a service layer. In our application we will have a Service interface called Contact. Service. Getting Started.
Get up to speed with Hiberante 3.5 and JPA 2.0 with this quick tutorial that will get you up and running, without any supurfluous talk or banter. If you want to learn Hibernate and JPA 2.0 quickly, this is the tutorial you need. Struts 2 and Hibernate Integration - Learn what is Basic Model View and Controller Architecture along with Struts 2 in simple and easy steps starting from Environment Setup, Architecture, Configuration, Actions, Interceptors. Welcome to Hibernate Search. The following chapter will guide you through the initial steps required to integrate Hibernate Search into an existing Hibernate enabled application. In case you are a new to Hibernate we recommend. Spring helps development teams everywhere build simple, portable, fast and flexible JVM-based systems and applications.
For our Contact Manager example, we will use My. SQL database. Create a table contacts in any My. SQL database. This is very preliminary example and thus we have minimum columns to represent a contact. Feel free to extend this example and create a more complex application. CREATE TABLE CONTACTS.
INT PRIMARY KEY AUTO. For this we will use the Maven Dynamic Web Project in Eclipse as the base architecture of our application. Download the below source code: Maven Dynamic Web Project (6. KB)Unzip the source code to your hard drive and import the project in Eclipse. Once the project is imported in Eclipse, we will create package structure for Java source.
One To Many Mapping in Hibernate. Hibernate One To Many Mapping Example Annotation, XML config, @OneToMany, @ManyToOne, @JoinColumn, foreign key mapping. JBoss Tools is an umbrella project for a set of Eclipse plugins that includes support for JBoss and related technologies, such as Hibernate, JBoss AS, CDI, Aerogear Hybrid Mobile, Apache Cordova, Docker, FeedHenry, JSF, (X. For this application we will also use Maven for build and. Tutorial: Struts2 Hibernate Application in Eclipse. In this article we will create a demo application (contact manager) to demonstrate use of Struts2 and Hibernate Framework in Eclipse. Hibernate Tutorial for Beginners. Hibernate Annotations Example. Please confirm that you want to add Spring and Hibernate for Beginners: Learn Step-By-Step to your Wishlist.
Create following packages under src/main/java folder. This package will contain Spring Controller classes for Contact Manager application. This package will contain form object for Contact manager application. Contact form will be a simple POJO class with different attributes such as firstname, lastname etc.
This package will contain code for service layer for our Contact manager application. The service layer will have one Contact. Service interface and its corresponding implementation classnet. This is the DAO layer of Contact manager application. It consists of Contact. DAO interface and its corresponding implementation class. The DAO layer will use Hibernate API to interact with database.
Entity Class – The Hibernate domain class. Let us start with the coding of Contact manager application. First we will create a form object or hibernate POJO class to store contact information. Also this class will be an Entity class and will be linked with CONTACTS table in database.
Create a java class Contact. File: src/main/java/net/viralpatel/contact/form/Contact. Column. import javax. Entity. import javax. Generated. Value.
Id. import javax. Table. @Table(name=& quot; CONTACTS& quot; ).
Contact . The first property in this class on line 1. ID which will be unique for all events persisted.
Create following Java files in net. File: src/main/java/net/viralpatel/contact/dao/Contact. DAO. javapackage net.
List. import net. Contact. public interface Contact. DAO . Classes marked with annotations are candidates for auto- detection by Spring when using annotation- based configuration and classpath scanning. The @Component annotation is the main stereotype that indicates that an annotated class is a “component”. The @Repository annotation is yet another stereotype that was introduced in Spring 2. This annotation is used to indicate that a class functions as a repository and needs to have exception translation applied transparently on it.
The benefit of exception translation is that the service layer only has to deal with exceptions from Spring’s Data. Access. Exception hierarchy, even when using plain JPA in the DAO classes. Another annotation used in Contact. DAOImpl is @Autowired. This is used to autowire the dependency of the Contact. DAOImpl on the Session.
Factory. The Service Layer. The Service layer of Contact Manager application consist of an interface Contact. Service and its corresponding implementation class Contact.
Service. Impl. Create following Java files in net. File: src/main/java/net/viralpatel/contact/service/Contact. Service. javapackage net. List. import net. Contact. public interface Contact. Service . Note that we used few Spring annotations such as @Service, @Autowired and @Transactional in our code. These annotations are called Spring stereotype annotations.
The @Service stereotype annotation used to decorate the Contact. Service. Impl class is a specialized form of the @Component annotation. It is appropriate to annotate the service- layer classes with @Service to facilitate processing by tools or anticipating any future service- specific capabilities that may be added to this annotation. Adding Spring MVC Support. Let us add Spring MVC support to our web application.
Update the web. xml file and add servlet mapping for org. Dispatcher. Servlet. Also note that we have mapped url / with spring.
Servlet so all the request are handled by spring. File: /src/webapp/WEB- INF/web. Once the web. xml is configured, let us add spring- servlet. WEB- INF folder. File: /src/main/webapp/WEB- INF/jdbc. The jdbc. properties file contains database connection information such as database url, username, password, driver class. You may want to edit the driverclass and dialect to other DB if you are not using My.
SQL. Oracle Properties. In case you are using Oracle database, you can modify the jdbc properties and have oracle related dialect and other properties: File: /src/main/webapp/WEB- INF/spring- servlet. The spring- servlet. For this bean we also set prefix as “/WEB- INF/jsp/” and suffix as “. Thus spring automatically resolves the JSP from WEB- INF/jsp folder and assigned suffix . Source bean – To provide Internationalization to our demo application, we defined bundle resource property file called messages.
Related: Internationalization in Spring MVCproperty. Configurer bean – This bean is used to load database property file jdbc.
The database connection details are stored in this file which is used in hibernate connection settings. Source bean – This is the java datasource used to connect to contact manager database. We provide jdbc driver class, username, password etc in configuration. Factory bean – This is Hibernate configuration where we define different hibernate settings.
File: /src/main/resources/hibernate. File: /src/main/resources/messages. Just add following Spring controller class Contact.
Controller. java to net. File: /src/main/java/net/viralpatel/contact/controller/Contact. Controller. javapackage net.
Map. import net. viralpatel. Contact. import net. Contact. Service. Autowired. import org.
Controller. import org. Binding. Result. import org. Model. Attribute. Path. Variable. import org. Request. Mapping. Request. Method. public class Contact. Controller . It returns an array of contacts.
Note that we have mapped request “/index” to this method. Thus Spring will automatically calls this method whenever it encounters this url in request. Contact method – This method adds a new contact to contact list. The contact details are fetched in Contact object using @Model. Attribute annotation. Also note that the request “/add” is mapped with this method.
The request method should also be POST. Once the contact is added in contact list using Contact. Service, we redirect to /index page which in turn calls list. Contacts() method to display contact list to user.
Related: Forms in Spring MVCdelete. Contact method – This methods removes a contact from the contact list. Similar to add. Contact this method also redirects user to /index page once the contact is removed. One this to note in this method is the way we have mapped request url using @Request.