Assignment 7 - AST

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Assignment 7

1. What is hibernate explain advantages of hibernate over JDBC.


Ans: Hibernate is a Java framework that simplifies the development of Java
application to interact with the database. It is an open source, lightweight, ORM
(Object Relational Mapping) tool. Hibernate implements the specifications of
JPA (Java Persistence API) for data persistence.

Advantages of hibernates:
 Hibernate supports Inheritance, Associations, Collections
 In hibernate if we save the derived class object, then its base class object
will also be stored into the database, it means hibernate
supporting inheritance
 Hibernate supports relationships like One-To-Many,One-To-One, Many-
To-Many-to-Many, Many-To-One
 This will also supports collections like List,Set,Map (Only new collections)
 In jdbc all exceptions are checked exceptions, so we must write code in
try, catch and throws, but in hibernate we only have Un-checked
exceptions, so no need to write try, catch, or no need to write
throws. Actually in hibernate we have the translator which converts
checked to Un-checked
 Hibernate has capability to generate primary keys automatically while we
are storing the records into database
 Hibernate has its own query language, i.e hibernate query language which
is database independent
 So if we change the database, then also our application will works as HQL
is database independent
 HQL contains database independent commands
 While we are inserting any record, if we don’t have any particular table in
the database, JDBC will rises an error like “View not exist”, and throws
exception, but in case of hibernate, if it not found any table in the
database this will create the table for us
 Hibernate supports caching mechanism by this, the number of round trips
between an application and the database will be reduced, by using this
caching technique an application performance will be increased
automatically.
 Hibernate supports annotations, apart from XML
 Hibernate provided Dialect classes, so we no need to write sql queries in
hibernate, instead we use the methods provided by that API.
 Getting pagination in hibernate is quite simple.

2. Explain architecture of hibernate.


Ans:

Configuration:
 Configuration is a class which is present in org.hibernate.cfg package. It
activates Hibernate framework. It reads both configuration file and mapping
files.
It activate Hibernate Framework
Configuration cfg=new Configuration();
It read both cfg file and mapping files
cfg.configure();
 It checks whether the config file is syntactically correct or not.
 If the config file is not valid then it will throw an exception. If it is valid then it
creates a meta-data in memory and returns the meta-data to object to
represent the config file.
SessionFactory:
 SessionFactory is an Interface which is present in org.hibernate package and it
is used to create Session Object.
 It is immutable and thread-safe in nature.
buildSessionFactory() method gathers the meta-data which is in the cfg Object.
From cfg object it takes the JDBC information and create a JDBC Connection.
SessionFactory factory=cfg.buildSessionFactory();
Session:
 Session is an interface which is present in org.hibernate package. Session
object is created based upon SessionFactory object i.e. factory.
 It opens the Connection/Session with Database software through Hibernate
Framework.
 It is a light-weight object and it is not thread-safe.
 Session object is used to perform CRUD operations.
Session session=factory.buildSession();
Transaction:
 Transaction object is used whenever we perform any operation and based
upon that operation there is some change in database.
 Transaction object is used to give the instruction to the database to make the
changes that happen because of operation as a permanent by using commit()
method.
Transaction tx=session.beginTransaction();
tx.commit();
Query:
 Query is an interface that present inside org.hibernate package.
 A Query instance is obtained by calling Session.createQuery().
 This interface exposes some extra functionality beyond that provided by
Session.iterate() and Session.find():
1. A particular page of the result set may be selected by calling
setMaxResults(), setFirstResult().
2. Named query parameters may be used.
Query query=session.createQuery();
Criteria:
 Criteria is a simplified API for retrieving entities by composing Criterion
objects.
 The Session is a factory for Criteria. Criterion instances are usually obtained via
the factory methods on Restrictions.
Criteria criteria=session.createCriteria();

You might also like