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 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 Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 minutes read.

Hibernate Mapping

Hibernate Mapping Association- It means that two or more things are related to each other by some specific relation. In other words, it specifies how the objects are associated with each other. Hibernate mapping is one...

2 minutes read.

Hibernate Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 minutes read.

Hibernate First-Level Cache

The first-level cache is related to the Session object. First level cache objects of one Session are not visible to the other Sessions, and all the cache data is lost when the Session...

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 Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

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

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.

Hibernate save() vs persist()

Session.save() The session.save() method is used to save the transient object by assigning a generated value or using a current value of the identifier property. In other words, the save() method is used...

3 minutes read.

Hibernate Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed. Cache provider The...

5 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 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 Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

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

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.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.