0% found this document useful (0 votes)
218 views

Hibernate Notes

The document provides an overview of Hibernate - an ORM framework for Java. It discusses how Hibernate allows Java objects to be mapped to database tables and handles SQL generation behind the scenes. The steps to set up a sample Hibernate project are outlined, including downloading required JAR files, creating a database table, and testing the JDBC connection. Key points covered are that Hibernate saves on writing JDBC code, supports object/relational mapping via XML or annotations, and allows objects to be saved, retrieved, queried and updated via its API.

Uploaded by

sasirekha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views

Hibernate Notes

The document provides an overview of Hibernate - an ORM framework for Java. It discusses how Hibernate allows Java objects to be mapped to database tables and handles SQL generation behind the scenes. The steps to set up a sample Hibernate project are outlined, including downloading required JAR files, creating a database table, and testing the JDBC connection. Key points covered are that Hibernate saves on writing JDBC code, supports object/relational mapping via XML or annotations, and allows objects to be saved, retrieved, queried and updated via its API.

Uploaded by

sasirekha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

===================================================================================

========================================================
1. HIBERNATE INTRODUCTION
===================================================================================
========================================================
Hibernate
~~~~~~~~~
1. Framework for persisting /saving java objects in db.
2. official site: hibernate.org

Flow
~~~~
Java App -> Hibernate-> database (for performing all operations).

Benefits
~~~~~~~~
1. Handles all of the low-level SQL
2. Minimize the amount of jdbc code that you have write.
3. ORM helps develpers mapping java classes and database table.
4. Mapping in hibernate is done by xml or annotations.

Saving a java object with Hibernate


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Student class with FN,LN and Email

//Create the java object


Student theStudent=new Student("Rekha" , "Vijayan", "rekhavijayan@gmail.com");

//save it into database


int theId=(Integer)session.save(theStudent);

session.save(theStudent)
~~~~~~~~~~~~~~~~~~~~~~~
At the background hibernate will store the data in to database(sql insert)

Retrieving a java object from database


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// now retrieve from database using the primary key
Student myStudent=session.get(Student.class,theId);
//Behind the scene hibernate will try to retrieve the student details based on ID.

Querying for java objects


~~~~~~~~~~~~~~~~~~~~~~~~~
Query query=session.CreateQuery("from Student");/* Gives you list of student object
*/
List<Student> students=query.list();(fetch the student object)

Here "from Student" -> HQL(Hibernate Query Language).

Conclusion
~~~~~~~~~~~
Using hibernate you can perform creating objects / Read Object / Delete object and
Update Object .

Interview Questions
~~~~~~~~~~~~~~~~~~~
1. How does hibernate relate to JDBC?
Hibernate uses JDBC for all database communications.
So hibernate is another layer of abstraction on top of JDBC.(i.e when your
application uses hibernate
framework,your app will store and retrieve objects using the Hibernate API.(In the
background
Hibernate does all of the low level JDBC work and submitting the SQL queries and so
on.

===================================================================================
=======================================================
2. HIBERNATE DEVELOPMENT
ENVIRONMENT OVERVIEW
===================================================================================
=======================================================
Software requirement
~~~~~~~~~~~~~~~~~~~~
1. JDK 8 (Hibernate 5.x jars need Java 8)

To build Hibernate application you need the following


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Eclipse / STS
Database server - mysql
Hibernate jar file and jdbc driver.

===================================================================================
==========================================================
3. INSTALLING MySQL ON
WINDOWS
===================================================================================
=============================================================
Step 1: download
Step 2: install
Step 3: verify

Download
--------
1. Go to browser and type https://dev.mysql.com/downloads/mysql/ -> click download
2. We are going to use mysql workbench.(GUI tool)
3. search for mysql workbench tools and login with user name root password root

===================================================================================
====================================================
4. SETUP DATABASE TABLES
===================================================================================
==================================================
sql scripts
-----------
Two script should be created
1.create-user.sql
2.student-tracker.sql

Step 1:
Create a new MySQL user for our application
userid : hbstudent
password:hbstudent

CREATE USER 'hbstudent'@'localhost' IDENTIFIED BY 'hbstudent';

GRANT ALL PRIVILEGES ON * . * TO 'hbstudent'@'localhost';

Now start a new connection in mysql by clicking +


connection name : hbstudent
username :hbstudent
click : Test connection
password : hbstudent

Step2: Create a new database table

CREATE DATABASE IF NOT EXISTS `hb_student_tracker`;


USE `hb_student_tracker`;

--
-- Table structure for table `student`
--

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

===================================================================================
=====================================
5. SETTING UP IN ECLIPSE
===================================================================================
=====================================
1. Create a java project
2. Download Hibernate jar files
3. Download mysql jdbc driver
4. Add jar files to Eclipse project

1. Create a new java project in STS


2. Create a new folder to put all the jar files
3. Download the jar files for hibernate
4. After downloading the file extract it and try to find required folder (Note :
This folder will have all the required
hibernate jar files).
C:\Users\psasirek\Documents\GitHub\Hibernate Framework\Hibernate Demos\hibernate-
release-5.4.10.Final\lib\required
5. copy the jars and paste it in lib folder of the project.
6. now we have to download mysql connector for java : visit the url and download
the connector
https://dev.mysql.com/downloads/connector/j/5.1.html
7. Add the jars to build path
8. right click project->properites->java build path->add jars->select your project-
>go to lib->select the jar files and click OK to
add these files in to build path.

===================================================================================
=========================================
6.
TEST YOUR JDBC CONNECTIONS
===================================================================================
==========================================
1. Create a simple java program.

2. Code snippets

//mysql updated 8 version we have to use it


String jdbcUrl="jdbc:mysql://localhost:3306/hb_student_tracker?
useSSL=false&serverTimezone=UTC";
String user="hbstudent";
String pass="hbstudent";
try {

System.out.println("Connecting to database"+jdbcUrl);
Connection myConn=
DriverManager.getConnection(jdbcUrl,user,pass);
System.out.println("connection success!!!");

}
catch(Exception ex) {
ex.printStackTrace();
}

===================================================================================
===============================================

You might also like