![]() |
Hibernate is a Java ORM (Object Relational Mapping) framework, that makes it easy to save Java objects to databases. It internally uses JPA (Java Persistence API) to persist the state of the object in the database schema. It does this by creating a link between your objects and database tables, so you don’t have to write lots of code to manage the data. It works with different databases, lets you do things like create, update, and delete data, and even builds the database structure for you automatically. It uses a hibernate session buffer which acts as an intermediate layer between the application and the database and caches the entities. It increases the performance significantly as the database is not hit each time an operation is performed. Inserts and updates are not made into the database immediately rather changes are saved into the session and the database is updated when the session is flushed. Advantages of HibernateGiven below are some advantages of using the hibernate framework:
Components of HibernateA Basic Hibernate program consists of mainly five major components:
Step by Step Implementation of Hibernate frameworkGiven program demonstrates creation of Employee table in the database and insertion of a record into it using Hibernate framework and MySQL database. Step 1: Download the jar files and include in your project. You can download the files from given link. https://jar-download.com/artifacts/org.hibernate/hibernate-core Alternatively, you can add following dependency in pom.xml for maven projects: <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.1.Final</version>
<type>pom</type>
</dependency>
Employee.java: Java
Step 3: Create resources folder and add hibernate mapping file for Employee in it. Employee.hbm.xml: XML
This file is used by Hibernate while creating the database schema. It consists of following tags:
Step 4: Create hibernate configuration file inside the resources folder. hibernate.cfg.xml: XML
This file is used to provide various Hibernate configurations like dialect, database driver, database url, database username, database password, hibernate mapping resources, etc. Step 5: Now, to add a record into the database, follow given steps: 1. Create Configuration object. Configuration config = new Configuration();
2. Provide hibernate configuration file to the Configuration object. By convention, hibernation configuration file is saved as ‘hibernate.cfg.xml’. config.configure("resources/hibernate.cfg.xml");
3. Build session factory using configuration object. SessionFactory sessionFactory = config.buildSessionFactory();
4. Open session and begin the transaction. Session session=sessionFactory.openSession();
Transaction t=session.beginTransaction();
5. Create new Employee object and save it using session. Employee emp = new Employee(101,"John");
session.save(emp);
6. Commit the transaction and close the session factory. t.commit();
sessionFactory.close()
Complete implementation of above steps is given below. SessionFactoryProvider.java:Create utilities folder and create a session factory provider class inside it. Java
This class creates a configuration object and provides hibernate configuration file to it. It returns the sessionFactory object which can later be used in the main class. Now, create the main class. Create.java:Java
Given class adds an entity in the employee table.Output:Run Create.java class. Open MySQL command line and view the inserted records. Use following queries to view the result: use demo;
select * from employee;
Complete Compilation and Execution of Program |
Reffered: https://www.geeksforgeeks.org
Advance Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |