Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Hibernate 3 Query Example Part 2

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

Ads

Tutorials   
When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials
Ads

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="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>

<head>

<title>All Articles</title>

</head>

<body>

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

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

<hr width=500 >

<table>

<tr>

<th>Article ID</th>

<th>Article Name</th>

<th>Article Description</th>

<th>Added Date</th>

</tr>

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

<tr>

<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>

</tr>

</c:forEach>

</table>

</c:if>

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

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

</c:if>

</body>

</html>

Step 11 :

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

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://www.springframework.org/tags/form"
 prefix="form"%>

<html>

<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." );

document.getElementById("articleid").focus();

return false;

}

if ( isNaN( articleid ) ){

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

return false;

}

return true;

}

</script>

<body>

<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"
  action
="searcharticle.html"

onsubmit="return Validate();">

<table align="center">

<tr>

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

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

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

</tr>

<tr>

<td></td>

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

</tr>

</table>

<br/>

</form:form>

</body>

</html>

Step 12 :

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

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http:
//java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>
org.springframework.web.servlet.DispatcherServlet

<//font>servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

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="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:property-placeholder location="classpath:jdbc.properties" />

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

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

<bean id="jspViewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass"

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

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

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

</bean>

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<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>

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

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

<property name="annotatedClasses">

<list>

<value>net.roseindia.model.Article</value>

</list>

</property>

<property name="hibernateProperties">

<props>

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

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

</props>

</property>

</bean>

<bean id="hibernateTransactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

</bean>

</beans>

Step 14 :

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

image

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

image

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

image 

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

image

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

image

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

image

Download Code

Download example code

Advertisements

Ads
Share on Google+Share on Google+

Hibernate 3 Query Example Part 2

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

Advertisements

Ads
 

Discuss: Hibernate 3 Query Example Part 2  

Post your Comment


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

Ads

 

Ads