Hibernate Notes
Hibernate Notes
========================================================
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.
session.save(theStudent)
~~~~~~~~~~~~~~~~~~~~~~~
At the background hibernate will store the data in to database(sql insert)
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)
===================================================================================
==========================================================
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
--
-- Table structure for table `student`
--
===================================================================================
=====================================
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
===================================================================================
=========================================
6.
TEST YOUR JDBC CONNECTIONS
===================================================================================
==========================================
1. Create a simple java program.
2. Code snippets
System.out.println("Connecting to database"+jdbcUrl);
Connection myConn=
DriverManager.getConnection(jdbcUrl,user,pass);
System.out.println("connection success!!!");
}
catch(Exception ex) {
ex.printStackTrace();
}
===================================================================================
===============================================