04 Month
01 Hours
JPA | Hibernate |
---|---|
java persistence Api is management of relational data in java application | Hibernate is object relation mapping
tool which store java object to relational database |
Entitymanager factory is used to interact with persistence unit | Session factory is used to Handel the persistence unit |
JPA is combination of multiple orm tool
i.e it specify orm tool by setting rules and guidelines |
Hibernate is an implementation of ORM |
JPA belongs to java. Persistence package | Hibernate belongs to org.hibernate package |
JPQL(Java persistence query language ) is used for db purpose | HQL(HIbernate query language) is used for db purpose |
JDBC | Hibernate |
---|---|
JDBC is java j2ee based database connectivity technology | Hibernate is framework to connect java object to relational database. |
it maintain db explicitly i.e call db
connection and transaction manage explicitly |
Hibernate itself manage all transaction |
does not support lezy loading | support lezy loading |
we need to write explicitly the code for cache management | Hibernate itself provide two ways of
caching i.e first level cache and second level cache |
Low in performance | Higher in performance |
Session | Session factory |
---|---|
Session is an interface which works as an object that maintain between database and java application |
Session factory is factory class that is used to get the session objects |
Session is not thread safe and non synch-ronised i.e. any no of tread can access it at same time |
Session factory is thread safe and synchronised i.e. only one thread can access it at one time |
Session supports first level cache | Session factory supports second level cache |
Session used to store methods such as save(),persist(), get(),load(),update(), merge() for different purpose |
For different session different session factory is created |
<?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 5.3//EN” “http://www.hibernate.org/dtd/hibernate-configuration-5.3.dtd”> <hibernate-configuration> <session-factory> <property name=”hbm2ddl.auto”>update</property> <property name=”dialect”>org.hibernate.dialect.Oracle9Dialect</property> <property name=”connection.url”>jdbc:oracle:thin:@localhost:1521:xe </property> <property name=”connection.username”>root</property> <property name=”connection.password”>admin</property> <property name=”connection.driver_class”>oracle.jdbc.driver.OracleDriver </property> </session-factory> </hibernate-configuration>
@Entity : this annotation is used in model class or entity class to define the base property or to identify the entity class.
@ID : this annotation is used to identify primary key value in the class independent of table defined or not , it is variable level annotation. used for id
@Table : when we already defined the table then this annotation is used to update table name manually ,if @table is not defined in the entity class hibernate create table automatically with same name as entity class.
@Column : when we already defined the table then this annotation is used to update Column name manually ,if @column is not defined in the entity class hibernate create table automatically with same name as entity class.
@GeneratedValue : it is used to define how generate the value for particular column ex: @GeneratedValue(strategy = GenerationType.IDENTITY) for auto_increament value we can use it
@Transient : This annotation denotes that such field is not mandatory to save in db or we can ignore such feilds .this is part of javax.persistence package. (Let’s say you have created two variables in an entity class in one you want to save in database and second is derived from the first one. So we need not save the second field in the database and can be map with @Transient annotation, the second field will be ignored to save in data base.)
@One to One : this annotation is used to show one to one relation between one object to the other object such as student and address
@One to many : this annotation is used to show one to many relation between one object to the other objects such as student and phone.
Get() | Load() |
---|---|
If the value not found, it returns null | If the value not found it returns object not found exception |
it return real object | it return the proxy object |
it always hits the database | it does not hits database always |
when we are not sure about existence of object that time get() method is preferred |
when we sure about existence of object that time load() method is preferred |
OpenSession() | GetCurrentSession() |
---|---|
opensession() create a new session and provide it to the user |
getcurrentsession() provide session object which are present in hibernate context and managed by hibernate internally |
Explicit call need to close the session | no need to explicit call to the session |
create one session per request | create new session if already exist |
Developer need to close the session | session close once the session factory get closed |
Single threaded application loading is slow in this method |
Single threaded application loading is faster in this method |
save() | saveupdate() | persist() |
---|---|---|
save() generate the identifier and insert it to database |
saveupdate() either insert or update value in db based on existence of object |
persist() also insert te value to db. |
save fails insertion if primary key already exists in the table |
if primary key are already there it will update the record |
-- |
return type is serializable | return type is void | return type is void |
used to bring transient object to persistence object |
used to bring both transient and detached object to persistence object |
performs within transient object |
only supported by hibernate |
Supported by JPA and hibernate |
Supported by JPA and hibernate |
To make any class immutable in hibernate, we can perform two ways:
it acts as a layer between application and database
, which reduce the loading time of
application as it fetches memory instead of loading
the data from db and used for the
load
similar type of data on multiple calls
There are two types of cache in Hibernate :
First level cache | Second level cache |
---|---|
Supported by session interface | Supported by session factory |
Enable by default | Enable through code only |
it gets memory when session started and once session closed automatically destroyed |
it gets memory with the code and destroyed once application closed or restarted |
There are three stage of object:
Lazy loading improves the performance
loading child class object along
with parent
class object