0% found this document useful (0 votes)
133 views28 pages

Hibernate With JPA

Uploaded by

webhackers00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views28 pages

Hibernate With JPA

Uploaded by

webhackers00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Hibernate with JPA

Drawbacks of JDBC

 The disadvantages of JDBC are:

• Lengthy code.

• SQL queries.

• Creating Table and setting the attributes.

• Programmer should follow 5 steps every time to perform crud


operations.

• Mapping is more difficult.


JPA (java persistence API)

1. JPA is a acronym of JAVA PERSISTENCE API.

2. JPA is a specification, not an implementation.

3. It is the standard application programming interface that makes database operations simple for developers to
carry out.

4. Hibernate is an implementation of the Java Persistence API (JPA).

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

1. ORM is an acronym of OBJECT-RELATIONAL MAPPING.

2. ORM acts as a converter between java object and a database.

3. ORM Handles the logic required to interact with databases.

4. You write less code when using ORM tools than with SQL.

5. There are some popular ORM tools:

Hibernate, Top Link, Eclipse Link ,Open JPA

6. The most used ORM tool is Hibernate.

7. The ORM tool internally uses the JDBC API to interact with the database
Hibernate

1. Hibernate is a Java framework that simplifies the development of Java application


to interact with the database
2. Hibernate is a open-source, light-weight, ORM tool .
3. Hibernate implements JPA specification for data persistence.
4. Hibernate framework can be used to create tables in the database automatically.
5. It can be used to perform all the CRUD operations without having to write SQL
queries.
Why 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

It uses JPQL (Java persistence query It uses Hibernate query language(HQL) to


language)as a object oriented programming perform database operations.
language to perform database operations.
To interconnect with the entity manager factory To create Session instances, it
for the persistence unit, it uses SessionFactory interface
uses EntityManagerFactory interface. Thus, it
gives an entity manager
It uses EntityManager to perform crud It uses Session interface to perform crud
operations like create, update, delete, read operations
• Create table ----------> by Annotate the class with @Entity
• Create columns-------> by having the attributes
• Create primary key---> by annotate the attribute with @Id

@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

 Mappings are done in two directions:-


1. unidirection
 One to one
 One to many
 Many to one
 Many to many

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;
}

Person Pan Card

Owning side Non Owing


Name Phon Card_ Id side
Name
ID e id PanNum
1 Yash 8798. 101 101 Yash dqu97…
. 102 Radhika Jkh88..
2 Radhika 68….. 102
One to Many uni-direction:

@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

id name phon e_id Non Owing


e side
c_id e_id id name sal
1 TY 9890. 101 1 101 101 King 1000
.
1 102 102 Queen 2000
1 TY 9890. 102
1 103 103 Minister 2000
.
1 TY 9890. 103
.
Many to One uni direction:

@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

ID Nam Phon Stu_ C_ID S_ID Non Owing


e e id 1 101 side
Id Name sal
1 java 3 101 1 102 101 Arya 1000
2 sql 1 102 2 102 102 Arjun 2000
3 html 1 103 2 103 103 Atharv 2000
3 101
3 103
To save/persist

public void savePerson(Person person) {

EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory(“sharath");
EntityManager entityManager=entityManagerFactory.createEntityManager();

entityTransaction.begin(); // transacation starts

entityManager.persist(person); //saving person object through persist() by passing person obj.

entityTransaction.commit(); //transaction ends

}
To delete

public void deletePerson(int id) {

EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory(“sharath");
EntityManager entityManager=entityManagerFactory.createEntityManager();

entityTransaction.begin(); // transacation starts

entityManager.remove(id); //delete theperson object through remove() by passing id

entityTransaction.commit(); //transaction ends

}
To find/fetch

public void findPerson(int id) {

EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory(“sharath");
EntityManager entityManager=entityManagerFactory.createEntityManager();

Person person1= entityManager.find(Person.class, id);

System.out.println(person1);

}
To update

public void findPerson(int id,Person person) {


EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory(“sharath");
EntityManager entityManager=entityManagerFactory.createEntityManager();
Person person1= entityManager.find(Person.class, id);
if(person1!=null) {

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

public String loginPerson(String mail) {

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

Id Nam Phon Car Id Name Phon


Id Nam Pho Car Id Na Pho P_Id e e d_Id e
e ne d_Id me ne 1 Dinga 8797 100 1001 Dinga 8797
1 Ding 8797 100 100 Ding 879 1 …. 1 ….
a …. 1 1 a 7…. 2 Dingi 7867 100 1002 Dingi 78678
2 Ding 7867 100 100 Ding 786 1 8687 2 687
i 8687 2 2 i 786 3 Rani 7678 100 1003 Rani 76786
3 Rani 7678 100 87 696 3 96
696 3 100 Rani 767 1
3 869
6
Thank you

You might also like