Hibernate Framework

Hibernate Framework
Techiio-author
Written by Sagar RabidasFebruary 14, 2022
6 min read
Hibernate
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Hibernate Framework

Hibernate Framework

This is how to hibernate connects to the relational database to execute queries:

  • Hibernate connects directly with the specified database and uses hibernate query language (HQL) to execute queries and map query results to java objects.
  • Hibernate uses properties set in Hibernate configuration XML file to map query results to java objects.
  • The database connection is created using a session that helps in saving and fetching the persistent java object.
  • The session is created using the Session factory interface. In an ideal case, there should be only one session factory per database.

Hibernate Framework Architecture

Hibernate follows the layered architecture and has the following layers:

  • Java application layer
  • Hibernate layer
  • Backend API Layer
  • Database Layer

Hibernate Configuration Object

This is the first object one has to create to establish a database connection using hibernate. It should be ideally created once, during application initialization. The configuration object provides the following:

  • Database Connection: Database connection is established using one or more configuration files. The files are hibernated .properties and hibernate.cfg.xml.
  • Mapping: This creates a mapping between java classes and relational database tables.

Session Factory

Configuration object created in step 1 is used to create a session factory object which makes hibernate configuration ready using the provided configuration file and makes way for session object to be created. Since the session factory is a heavy object, it is usually created once during the starting phase of the application. There is a need for multiple session factory object in case connections to multiple databases needs to be established. Also, the session factory is a thread-safe object.

Session:-

The session object establishes a physical connection with the database. It is a lightweight object and should be created each time interaction with the database is required. If an object needs to be persisted or needs to be retrieved, it can only be done using the session object. Session objects should be closed as soon as the required operation is completed because they do not thread-safe.

Transaction:-

It is an optional object and represents a unit of work done with the database. A transaction object ensures that either all operations must execute or none of them must execute. It is a single-threaded and short-lived object.

Query Object

This object uses structured query language (SQL) or Hibernate Query Language (HQL) to fetch data from the database and instantiate objects. A Query object can be used to limit output returned from the query, bind query parameters, and execute the query.

Queries

Here we will execute some queries that will make things more clear. Let us consider an Entity Employee having class structured as:

Code:

Package com.edubca.hibernatetest;
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable
{
private static final long serialVersionUID = -1798070786993123455L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "empID")
private Integer empID;
@Column(name = "NAME")
private String empName;
@Column(name = "SALARY")
private Integer salary;
//Getters and setters
}

Hibernate requires an XML file called hibernate.cfg.xml that looks like:

Code:

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
<property name="hibernate.connection.password"> edubca</property>
<property name="hibernate.connection.username">edubcauser</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.edubca.hibernatetest.Employee"></mapping>
</session-factory>
</hibernate-configuration>

Below is the code to show how insertion and retrieval take place into the database using hibernate:

Code:

//Create Configuration object
Configuration con=new AnnotationConfiguration().configure(new File("hibernate.cgf.xml"));
// create session factory using configuration
SessionFactory fact=conf.buildSessionFactory();
//get session from session factory
Session session=fact.openSession();
//Instantiate and populate Employee entity object
Employee emp=new Employee();
emp.setempID(1);
emp.setempName(“Yash”);
emp.setSalary(40000);
Employee emp1=new Employee();
emp1.setempID(2);
emp1.setempName(“Aman”);
emp1.setSalary(42000);
//persist emp object
session.save(emp);
//persist emp1 object
session.save(emp1);
//retrieve data from database
Query query=session.createQuery(“from Employee”);
List<Employee> list= query.list();
For(Employee e : list){
System.out.println(“Employee with ID ” + e.getempID() + “ has Name ” + e.getempName() + “ has salary ” + e.getsalary());
}
Hibernate
Hibernate Framework
Java
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance