Table of contents
1.
Introduction
2.
index.jsp
3.
register.jsp
4.
student.java
5.
studentDao.java
6.
student.hbm.xml
7.
hibernate.cfg.xml
8.
OUTPUT
9.
Frequently Asked Questions
9.1.
What distinguishes spring boot from hibernate?
9.2.
What are ORM and JPA?
9.3.
What does Hibernate's lazy loading mean?
9.4.
What is the use of generator class in hibernate?
9.5.
What is Association in Hibernate?
10.
Conclusion
Last Updated: Mar 27, 2024

HB Web application

Author Komal Shaw
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hibernate will be used in this instance to build a web application. We use JSP for presentation logic, Bean class for data representation, and DAO class for database codes when creating the HB web application.

Hibernate's straightforward application creation process eliminates the need for additional activities while developing a web application. In this situation, the JSP file is used to retrieve the value from the user.

Let's look at an example of HB Web application.

index.jsp

This page receives user input and uses the post method to transmit it to the register.jsp file.

<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

This file collects all request parameters and saves them in a User class object. Additionally, it uses the User class object while calling the register method of the UserDao class.

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

student.java

It's a class for beans. All of the variables in the bean class have public setter and getter methods and are set to private.

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 includes the method to store the object of the 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 associates the database table with the Student class object.

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

hibernate.cfg.xml

It includes details about the relational database and mapping file.

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

Output

Frequently Asked Questions

What distinguishes spring boot from hibernate?

While Hibernate framework is suitable for object-relational persistence, access data layers, and query retrieval services for enterprise-level applications, the Spring framework is helpful for transaction management, dependency injection, and aspect-oriented programming for applications.

What are ORM and JPA?

Java objects are changed into database tables through a technique known as object-relational mapping (ORM). In other words, this enables SQL-free interaction with a relational database. How to persist data in Java programs is specified by the Java Persistence API (JPA) specification.

What does Hibernate's lazy loading mean?

Lazy setting is used to determine whether to load child objects while loading the parent object This setting must be made in the parent class's appropriate hibernate mapping file. True = Lazy (means not to load child) then the child objects' sluggish loading is enabled by default.

What is the use of generator class in hibernate?

When creating an ID for an item that will be used as a primary key in the database, a generator class is utilized. The persistent class's objects have an ID as a special identification. According to our needs, we can use any generator classes in our program.

What is Association in Hibernate?

One of the main components of JPA and Hibernate is association mapping. In your domain model, they represent the connection between two database tables as attributes. This lets you explore your domain model's associations, and JPQL or Criteria queries easily.

Conclusion

In this article, we have extensively discussed the HB Web application.

We hope this blog has helped you enhance your knowledge regarding HB Web application. If you want to see more of our articles like HB Web application, check out our articles on Hibernate ArchitectureHibernate ConfigurationHB Generator classes.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc.

Enrol in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass