Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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
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.