JPA With Hibernate
JPA With Hibernate
-------------------------
JPA Application:-
---------------------
any java application, that uses JPA api to perform persistnce operation (CRUD )
operation with
the DB s/w is called as JPA application.
JPA architecture:-
--------------------
--if we are using the annotaion, then we need not map this class with the table
inside the xml mapping file.
--this class has many instance variables should be there as same as columns in the
corresponding table
--we need to provide mapping information with the table in this class only using
annotaitons.
Note:- when we gives this persistance /Entity class obj to the ORM s/w, then ORM
s/w will
decide the destination DB s/w based on the configuration done in a xml file which
is called as hibernate-configuration file.
Configuration file:-
-----------------------
--this file must be created under src/META-INF folder in normal java application,
where as in maven or gradle based application this file should be inside the
src/main/resources/META-INF folder
--this file content will be used by ORM s/w (ORM engine) to locate the destination
DB s/w.
2.ORM specific details (some instruction to the ORM s/w like dialect
info,show_sql ,etc)
--we should always create this configuration file by taking support of example
applications inside
the project folder of hibernate download zip file or by taking the reffernce from
the Google.
ex:-
persistence.xml:-
--------------------
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<class>com.ratan.Student</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/ratandb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root />
/*
<property name="hibernate.connection.driver_class"
value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/ratandb"/>
*/
</properties>
</persistence-unit>
</persistence>
1. <class> tag ,:-using which we specify the Entity class name(fully qualified
name) that used
annotations to map a table
2.<properties> tag :- using this tag,we specify some configuration details to the
ORM s/w
ORM engine :-
-----------------
--it is a specialized s/w written in java that performs translation of jpa calls
into the sql call by using mapping annotation and configuration file details and
send the mapped sql to the DB s/w using JDBC.
1.create a maven project and add the hibernate-core dependency to the pom.xml.
step 4:- create as many Entity/Perssitence classes as there r tables in the DB,
apply the at least 2 annotations to these classes
--if we apply above 2 annotations then our java bean class will become Entity or
Persistence class.
step 5:- create a client application and activate ORM engine by using JPA api
related following classes and interface and perform the DB operations.
1.Persistence class
2.EntityManagerFactory
3.EntityManager
Configuration class
SessionFactory(I)
Session(I)
connection pool,
--by using this EMF class only we create the EntityManager object.
Note:- inside every DAO method we need to get the EntityManager obj
--in order to perform any DML (insert update delete ) the method calls should be in
a transactional area.
this EntityTransaction obj is a singleton object, i.e per EntityManager obj, only
one Transaction object is created.
--to store the object we need to call persist(-) method on the EM object.
--to get the Object from the DB we need to call :- find(--) method of EM object
ex:-
EntityManagerFactory
emf=Persistence.createEntityManagerFactory("studentUnit");
if(s != null)
System.out.println(s);
else
System.out.println("Student does not exit..");
em.close();
EntityManagerFactory
emf=Persistence.createEntityManagerFactory("studentUnit");
et.begin();
em.persist(s);
et.commit();
*/
em.getTransaction().begin();
em.persist(s);
em.getTransaction().commit();
em.close();
System.out.println("done");
Main.java:- Delete:-
-------------------------
EntityManagerFactory
emf=Persistence.createEntityManagerFactory("studentUnit");
if(student != null){
em.getTransaction().begin();
em.remove(student);
em.getTransaction().commit();
System.out.println("Student removed....");
}else
System.out.println("Student not found...");
em.close();
System.out.println("done");