Share on Google+Share on Google+

Hibernate 3 Query Example Part 2

This tutorial explains how to create Hibernate 3 query example with Spring 3 MVC based annotations.

Hibernate 3 Query Example Part 2


Hibernate 3 Query Example Part 2

This tutorial  explains  how to create  Hibernate 3 query example with Spring 3  MVC based annotations. This example is created in ellipse IDE and run in tomcat server.

We will also show you how to run the example from Eclipse IDE and test on Tomcat server. We have also given the screen shot of the example. You can easily download Hibernate 3 Query example and run on your computer.

This example is very helpful for beginners in learning concepts of Hibernate and Spring.

Step 10 :

Now Create "view" folder under "WebContent/WEB-INF" .Again create one jsp file "articlesList.jsp" in "view" folder . The code of  "articlesList.jsp"  given below as :

<%@ taglib uri="" prefix="c"%>



<title>All Articles</title>



<c:if test="${!empty articles}">

<h1 align="center">List Articles</h1>

<hr width=500 >



<th>Article ID</th>

<th>Article Name</th>

<th>Article Description</th>

<th>Added Date</th>


<c:forEach items="${articles}" var="article">


<td><c:out value="${article.articleId}"/></td>

<td><c:out value="${article.articleName}"/></td>

<td><c:out value="${article.articleDesc}"/></td>

<td><c:out value="${article.addedDate}"/></td>





<c:if test="${empty articles}">

<c:out value="Not Available Article Details"></c:out>




Step 11 :

Now create "searcharticle.jsp" jsp file under "WebContent/WEB-INF/view" directory . The code of  "searcharticle.jsp" given below :

<%@ taglib uri="" prefix="c"%>

<%@ taglib uri=""


<head><title>Add Article</title></head>

<script language="JavaScript">

function Validate(){

var articleid =document.getElementById("articleid").value;

if(articleid=="" || articleid.length<1){

alert( "Please enter article id." );


return false;


if ( isNaN( articleid ) ){

alert( "Please enter a valid article id." );

return false;


return true;




<h1 align="center">Search Article</h1>

<hr width=500 >

<c:url var="viewArticlesUrl" value="/articles.html" />

<br />

<br />

<c:url var="saveArticleUrl" value="/articles/save.html" />

<form:form modelAttribute="article" method="POST"

onsubmit="return Validate();">

<table align="center">


<td><form:label path="articleId">Article Id:</form:label></td>

<td><form:input path="articleId" id="articleid" /></td>

<form:errors path="articleId" > </form:errors>




<td><input type="submit" value="Search" /></td>







Step 12 :

Now open  "web.xml" file  and modify as :

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi=""

xmlns="" xmlns:web=

xsi:schemaLocation=" http:
















Step 13 :

Now create "dispatcher-servlet.xml" file  under "WebContent/WEB-INF" directory . The code of  "dispatcher-servlet.xml" given below as :

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""





<context:property-placeholder location="" />

<context:component-scan base-package="net.roseindia" />

<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

<bean id="jspViewResolver"


<property name="viewClass"

value="org.springframework.web.servlet.view.JstlView" />

<property name="prefix" value="/WEB-INF/view/" />

<property name="suffix" value=".jsp" />


<bean id="dataSource"


<property name="driverClassName" value="${database.driver}" />

<property name="url" value="${database.url}" />

<property name="username" value="${database.user}" />

<property name="password" value="${database.password}" />


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation

<property name="dataSource" ref="dataSource" />

<property name="annotatedClasses">





<property name="hibernateProperties">


<prop key="hibernate.dialect">${hibernate.dialect}</prop>

<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>




<bean id="hibernateTransactionManager"


<property name="sessionFactory" ref="sessionFactory" />



Step 14 :

When run "Hibernate3QueryExample" on tomcat server display output as :


When click hyperlink "List of Articles" display output as :


If click hyperlink "Search Article" then open the search article page as :


If click "Search" Button without entering any number, it displays error message as :


If you enter any invalid number then it displays error message as :


If you enter article id that exists in table, then it displays article details as :


Download Code

Download example code



Posted on: April 5, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate 3 Query Example Part 2  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image