Web Application with Hibernate

Here, we are going to create a web application with hibernate. For creating the web application, we are using JSP for presentation rationale, Bean class for representing data and DAO class for database codes.

As we create the simple application in hibernate, we don't need to perform any extra operations in hibernate for creating web application.

In such case, we are getting the value from the user using the JSP file.

Example to create web application using hibernate

index.jsp

	
<form action="register.jsp" method="post">
Name:<input type="text" name="name"/><br><br/>
Password:<input type="password" name="password"/><br><br/>
Email ID:<input type="text" name="email"/><br><br/>
<input type="submit" value="register"/>"
</form>
	

register.jsp

	
	<%@page import="com.javatportal.mypack.UserDao"%>
<jsp:useBean id="obj" class="com.javatportal.mypack.User">
</jsp:useBean>
<jsp:setProperty property="*" name="obj"/>

<%
int i=UserDao.register(obj);
if(i>0)
out.print("You are successfully registered");

%>
	

User.java

It is the simple bean class representing the Persistent class in hibernate.

	
	package com.javatportal.mypack;

public class User {
private int id;
private String name,password,email;

//getters and setters
}

>user.hbm.xml


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 <hibernate-mapping>
 <class name="com.javatportal.mypack.User" table="u400">
 <id name="id">
 <generator class="increment"></generator>
 </id>
 <property name="name"></property>
 <property name="password"></property>
 <property name="email"></property>
 </class>
       
 </hibernate-mapping>

UserDao.java


package com.javatportal.mypack;


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class UserDao {

public static int register(User u){
 int i=0;
 Session session=new Configuration().
  configure().buildSessionFactory().openSession();
		
  Transaction t=session.beginTransaction();
  t.begin();
				
  i=(Integer)session.save(u);
				
  t.commit();
  session.close();
  
  return i;
 }
}

hibernate.cfg.xml

It is a configuration file, containing informations about the database and mapping file.


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
 <property name="hbm2ddl.auto">create</property>
 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
 <property name="connection.username">system</property>
 <property name="connection.password">oracle</property>
 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    
 <mapping resource="user.hbm.xml"/>
 </session-factory>

</hibernate-configuration>