Hibernate With JPA
Hibernate With JPA
Drawbacks of JDBC
• Lengthy code.
• SQL queries.
3. It is the standard application programming interface that makes database operations simple for developers to
carry out.
5. JPA (Java Persistence API) is a specification of Java, which is used to access, manage, and persist data between
Java object and relational database.
6. As JPA is a specification it does not perform any operations by itself ,thus it requires implementation so ORM
tools like Hibernate, ibatis, Toplink, EclipseLink implements JPA specification for data persistence.
7. JPA specification is same for all the ORM tools and it follows same standards ,so in the future if we want to
switch our application from one ORM tool to another then we can do it easily.
ORM
4. You write less code when using ORM tools than with SQL.
7. The ORM tool internally uses the JDBC API to interact with the database
Hibernate
1. Hibernate reduces the lines of code by maintaining object-table by mapping itself and
returns result to application in form of Java objects.
2. Hibernate does the implementation internally like writing queries & establishing connection
etc.
3. Hibernate removes a lot of boiler-plate code that comes with JDBC API, the code looks more
cleaner and readable.
4. Hibernate supports inheritance, associations and collections. These features are not present
with JDBC API.
5. JDBC is database dependent i.e. one needs to write different codes for different database.
Whereas Hibernate is database independent and same code can work for many databases
with minor changes.
JDBC API
Java Application
ORM(hibernate)
database
JPA(JAVA PERSISTENCE HIBERNATE
API)
JPA manages the relational databases in java Hibernate is an ORM tool which is used to save
applications. the state of the object into the database.
JPA is a specification various ORM tools Hibernate is an implementation and it’s a most
implement it for data persistence. used ORM tool.
It is defined in javax.persistence package It is defined in org.hibernate package
@Entity
Employee
Class Employee{
@Id
Private int id; id name phone
Private String name;
•1 • Yash • 987654321
Private long phone; •2 • Radhika 0
} • 912345678
7
Components
Persistence:
EntityManagerFactory:
EntityManager:
EntityTransaction:
Query
Save the persistence.xml file in
same format .
Persistence.xml
Its a configuration file in which src/main/
database is supported to perform resource
save ,update and delete the entity
objects for java API.
It’s a Standard configuration file in JPA
which is used to configure persistence META-INF
settings.
It is used to define the persistence unit
which is a collection of entity classes
that are managed by a single Persistence.xm
EntityManager. l
It contains main elements like
persistence unit name ,properties like
JDBC url , username, password for
database and mapping files which
maps between entity classes and
database tables.
Persistence
Is a Bootstrap class
It is used to create and return the EntityManagerFactory for
the named persistent unit.
It mainly depends on EntityManagerFactory and Persistence
unit name.
Persistent unit name is available in persistence.xml file .Its a
configuration file in which database is supported to do
save ,update, delete the entity objects for java Api.
EntityManagerFactory
Its an interface.
Present in javax.persistence package.
This Interface is used to interact with the EntityManagerFactory for
the persistence unit.
Persistence.createEntityManagerFactory(“persistent unit name”);
create and return an EntittyManagerFactory for named persistent
unit name.
EntityManager
Its an interface.
Present in javax.persistence package.
This Interface used to interact with the entity manager factory for the
persistence unit.
Is used to read , write and delete an entity .
entityManagerFactory.createEntityManager();
this method is used to create entitymanager instance each time
when it is invoked
EntityTransaction
Its an interface.
Present in javax.persistence package.
Used to control transactions on resource local entity managers.
Interface provide important methods to handle transaction in JPA based
applications.
entityManager.getTransaction(); Return the resource-level Entity
Transaction object. The Entity Transaction instance may be used
serially to begin and commit multiple transactions.
entityTransaction.begin();----------> start a resource transaction
entityTransaction.commit();------->Commit the current resource
transaction, writing any unflushed changes to the database.
Mapping
2. bidirection
One to one
One to many /many to one
Many to many
One to One uni-direction:
@Entity
Class Person{
@Entity
@id Class PanCard{
Private int id; @id
Private String name; Private int id;
Private long phone; Private String
@OneToOne name;
Private PanCard card; Private String
} panNum;
}
@Entity
@Entity
Class Company{
@id Class Employee{
Private int id; @id
Private String name; Private int id;
Private long phone; Private String
@OneToMany name;
Private Employee em; Private double sal;
} }
Company Employee
Owning side
@Entity @Entity
Class Employee{ Class Company{
@id @id
Private int id;
Private int id;
Private String name;
Private String
Private long phone;
@ManyToOne name;
Private Company c; Private long phone;
} }
Employee Company
Owning side
id name Phon Com_ Non Owing
e id Id side
Name phone
101 King 8798. 1 1 Ty 43587…
.
102 Queen 6987. 1
.
Many to Many uni direction:
@Entity
@Entity
Class Course{
@id Class student{
Private int id; @id
Private String name; Private int id;
Private int duration; Private String
@ManytoMany name;
Private Student st; Private long phone;
} }
Course Student
Owning side
EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory(“sharath");
EntityManager entityManager=entityManagerFactory.createEntityManager();
}
To delete
EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory(“sharath");
EntityManager entityManager=entityManagerFactory.createEntityManager();
}
To find/fetch
EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory(“sharath");
EntityManager entityManager=entityManagerFactory.createEntityManager();
System.out.println(person1);
}
To update
person.setMail(mail);
person.setFirst_Name(person1.getFirst_Name());
person.setPhone(person1.getPhone());
entityTransaction.begin();
entityManager.merge(person);
entityTransaction.commit();
}
Query
It’s a interface
Present in javax.persistence package
Interface is used to control query execution.
entityManager.createQuery(jpql); ------ create an instance of query to
execute
Used to create JPQL query and can call the object according to the query
If the Query is wrong it throws a illegalArgumentexception.
It accepts only string format parameter.
It provides many methods to retrieve the objects.
LogIn
EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory(“sharath");
EntityManager entityManager=entityManagerFactory.createEntityManager();
String jpql="select a from Person a where a.mail=?1";
Query query=entityManager.createQuery(jpql);
query.setParameter(1, mail);
Person person=(Person)query.getSingleResult();
return person.getPwd();
}
One to one bi direction:
@Entity @Entity
@Entity
Class PanCard{ Class Person{ Class PanCard{
@Entity @id @id
@id
Class Person{ Private int id; Private int id;
@id Private int id;
Private String Private String name; Private String
Private int id;
name; Private long phone; name;
Private String name; @onetoone
Private long phone; Private String Private String
@joincolumn panNum;
@onetoone panNum; Private PanCard
Private PanCard card; @onetoone @onetoone(mapp
card;
} Private Person p; edby=“card”)
}
} Private Person p;
}
Pan Card person pancard
Person