Hibernate

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

LEARNCODEWITH DURGESH

PREREQUISITE

Java Core JDBC Database

Basic concepts Basics of


database
are very Basic JDBC API tables, keys
important and queries
WRITE CODE
IS A BEST SOLUTION TO LEARN
PERFECTLY
ABOUT
THIS
TUTORIAL
• SUBSCRIBE
LEARNCODEWITH
• LIKE
DURGESH
• And SHARE
LEARNCODEWITH DURGESH
HIBERNATE FRAMEWORK
• Hibernate is a Java framework that simplifies the development of Java application to interact
with the database.
• Hibernate is ORM (Object Relational Mapping) tool.
• Hibernate is an Open source, lightweight.
• Hibernate is a non-invasive framework, means it won't forces the programmers to
extend/implement any class/interface.
• It is invented by Gavin King in 2001.
• Any type of application can build with Hibernate Framework.
TRADITIONAL WAY TO SAVE
DATA(JDBC)

Objects
JDBC

TABLE

WE write code manually to store objects(data) to


database using jdbc
WHERE HIBERNATE PLAY ITS ROLE
class
{ ORM

HIBERNATE
Objects

TABLE

Now it is done automatically by hibernate….


ORM( Object Relational Mapping)
COMMONLY USE HIBERNATE
ANNOTATIONS
• @Entity – use to mark any class as Entity.
• @Table – use to change the table details.
• @Id- use to mark column as id(primary key).
• @GeneratedValue- hibernate will automatically generate values for that using an internal sequence.
Therefore we don’t have to set it manually.
• @Column-Can be used to specify column mappings. For example, to change the column name in the
associated table in database.
• @Transient-This tells hibernate not to save this field.
• @Temporal- @Temporal over a date field tells hibernate the format in which the date needs to be saved
• @Lob-@Lob tells hibernate that this is a large object, not a simple object.
• @OneToOne , @OneToMany , @ManyToOne, @JoinColumn etc.
FETCH DATA
get( ) load()
get method of Hibernate Session returns load() method throws
null if object is not found in cache as well as ObjectNotFoundException if object is not
on database. found on cache as well as on database but
never return null.

get() involves database hit if object doesn't load method can return proxy in place and
exists in Session Cache and returns a fully only initialize the object or hit the database
initialized object which may involve several if any method other than getId() is called on
database call persistent or entity object. This lazy
initialization
increases the performance.

Use if you are not sure that object exists in Use if you are sure that object exists.
db or not
MANY TO MANY MAPPING
ONE TO MANY MAPPING
question_id question
Foreign key
12 What is Java?

13 What is python?

123 How networking works?

Question answer_id answer q_id


87 Java is ….. 12
3 Hibernate… 12
13 Python is … 13
42 ML….. 13
35 Django…. 13
Answer
MANY TO MANY MAPPING
eid ename pid project_name
2 Library Management
12 Ram
3 Chatbot
13 Shyam 13 Ecom website
42 School management
123 Sunder
35 Online booking
EMP PROJECT

Eid pid

12 2

13 2

13 3

EMP_PROJECT
FETCH TYPE

Answers

A1 A2 A3 A4

Questions
@Entity
public class Question {

@Id
@Column(name = "question_id")
private int questionId;

private String question;

@OneToMany(mappedBy = "question")
private List<Answer> answers;
}
FETCH TYPE

LAZY EAGER
In Lazy loading, associated It is a design pattern in
data loads only when we which data loading occurs
explicitly call getter or on the spot.
size method.
Transient Persistent
State State

Detached Removed
State State
Transient Persistent
State State

database

Detached Removed
State State

Session Object
HQL
Hibernate Query Language
How to get the data in hibernate?

get( ) load( )
How to load complex
data ?
HQL SQL
• Database independent • Database dependent
• Easy to learn for programmer. • Easy to learn for DBA.

• from Student • Select * from Student

Table Name
Entity Name
CACHING IN HIBERNATE
Caching is a mechanism to
enhance the performance of a
Application.

Cache is use to reduce the


number of database queries.
USE CASE

JAVA
APPLICATION Database
NOW CACHING COMES

JAVA
APPLICATION Database
HIBERNATE CACHING

FIRST LEVEL SECOND LEVEL

Session Object SessionFactory


By default Manually
Provide Enable
HIBERNATE WITH SPRING

TodoDao HibernateTemplate
save(todo)
getAll() SessionFactory

DriverManagerDataSource LocalSessionFactoryBean

DataSource

You might also like