Hibernate
Hibernate
Hibernate
PREREQUISITE
Objects
JDBC
TABLE
HIBERNATE
Objects
TABLE
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?
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;
@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.
Table Name
Entity Name
CACHING IN HIBERNATE
Caching is a mechanism to
enhance the performance of a
Application.
JAVA
APPLICATION Database
NOW CACHING COMES
JAVA
APPLICATION Database
HIBERNATE CACHING
TodoDao HibernateTemplate
save(todo)
getAll() SessionFactory
DriverManagerDataSource LocalSessionFactoryBean
DataSource