Hibernate Web Application Example

Web Application Example with Hibernate

In this section, we create a hibernate web Application. Here we are using a JSP file for presentation.

Example of creating a web application using hibernate

index.jsp

This page will show a registration form. It takes the input from the form and sends it to the associated (in this case, register.jsp) page.

Name: Mobile No: Password: Email ID:

register.jsp

The register.jsp will get all the information from index.jsp and stores it in the object of Student class.

<%@page import="com.hibernate.webap.StudentDao"%>
 
 
  
 <%
 int i=StudentDao.register(s);
 if(i>0)
 out.print("Registration is successfull");
 %> 

Student.java

It is a bean class. All the variables of bean class are set as private with public setter and getter methods.

public class Student {
  private int id,phnno;
  private String name,email,password;
  public int getId() { 
  return id;
  }
  public void setId(int id) {
  this.id = id; 
  }
  public int getPhnno() {
  return phnno;
  } 
  public void setPhnno(int phnno) {
  this.phnno = phnno;
  } 
  public String getName() {
  return name;
  } 
  public void setName(String name) {
  this.name = name;
  } 
  public String getEmail() {
  return email;
  } 
  public void setEmail(String email) {
  this.email = email;
  } 
  public void setPassword(String password) {
  this.password = password;
  }
 } 

StudentDao.java

It contains the method to store the object of Student class.

import org.hibernate.Session; 
 import org.hibernate.SessionFactory; 
 import org.hibernate.Transaction; 
 public class StudentDao { 
 public static int register(Student student){  
 int i=0; 
 Configuration cfg= new Configuration();
 cfg.configure("hibernate.cfg.xml");
 SessionFactory factory= cfg.buildSessionFactory(); 
 Session session = factory.openSession(); 
 Transaction t = session.beginTransaction(); 
 i=(Integer)session.save(student); 
 t.commit();  
 session.close(); 
 return i; 
  } 
  }  

student.hbm.xml

It maps the object of Student class with the database table.

 
 http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd"> 
   
  
  
   
  
  
  
  
  
  
   

hibernate.cfg.xml

It contains information about the mapping file and relational database.


 http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd"> 
 
 
 create
 org.hibernate.dialect.Oracle9Dialect 
 oracle.jdbc.driver.OracleDriver
 jdbc:oracle:thin:@localhost:1521:xe
 root //write your own username
 root //write your own password 
 
 
    

Output

Web Application Example with Hibernate
Web Application  Hibernate

Related Topics

Hibernate Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

4 minutes read.

Hibernate Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

3 minutes read.

Hibernate GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how to...

3 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Hibernate vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 minutes read.

Hibernate vs. JDBC

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

2 minutes read.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Architecture of Hibernate

Next → Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc.  There are mainly three layers in Hibernate...

2 minutes read.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.

Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child...

3 minutes read.

Hibernate History and Versions

Next → History of Hibernate Hibernate was developed in 2001 by Gavin king with his colleagues from Circus Technologies. The main aim was to provide better persistence capabilities than those of EJB2, by reducing the...

2 minutes read.

Hibernate Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in...

4 minutes read.

Hibernate Features

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

2 minutes read.

Hibernate Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 minutes read.

Hibernate Many-to-Many Mapping

Many-to-Many Hibernate Mapping with Example In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to...

3 minutes read.

Hibernate SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.

Hibernate Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

3 minutes read.

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 minutes read.