Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

HIBERNATE FRAMEWORK- Hibernate is a Java framework that simplifies the development of Java

application to interact with the database. It is an open source, lightweight, ORM tool. Hibernate
implements the specifications of JPA (Java Persistence API) for data persistence. ADVANTAGES:
Open Source & Lightweight-Hibernate FW is open source under the LGPL license & lightweight.
Fast Performance-The performance of hibernate framework is fast because cache is internally
used in hibernate framework. There are two types of cache in hibernate framework first level
cache & second level cache. First level cache is enabled by default. Database Independent Query-
HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database
independent queries. So you don't need to write database specific queries. Before Hibernate, if
database is changed for the project, we need to change the SQL query as well that leads to the
maintenance problem. Automatic Table Creation-Hibernate framework provides the facility to
create the tables of the database automatically. So there is no need to create tables in the
database manually. Simplifies Complex Join-Fetching data from multiple tables is easy in hibernate
framework. Provides Query Statistics & Database status-Hibernate supports Query cache &
provide statistics about query & database status. ARCHITECTURE: The Hibernate architecture
includes many objects such as persistent object, session factory, transaction factory, connection
factory, session, transaction etc. LAYERS: Java Application Layer, Hibernate FW Layer , Backhand
API, Database Layer
ELEMENTS OF ARCHITECTURE: 1.SessionFactory-The SessionFactory is a factory of session and
client of ConnectionProvider. It holds second level cache of data. The org.hibernate.SessionFactory
interface provides factory method to get the object of Session. 2.Session-The session object
provides an interface B/W the application & data stored in the database. It is a short-lived object &
wraps the JDBC connection. It is factory of Transaction, Query & Criteria. It holds a first-level
cacheof data. The org.hibernate.Session interface provides methods to insert, update & delete the
object. It also provides factory methods for Transaction, Query & Criteria. 3.Transaction-The
transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction
interface provides methods for transaction management. 4.ConnectionProvider-It is a factory of
JDBC connections. It abstracts the application from DriverManager or DataSource. It is optional.
5.TransactionFactory- It is a factory of Transaction. It is optional.
HIBERNATE WITH IDE(ECLIPSE): Steps: 1.Create the java project. 2.Add jar files for hibernate.
3.Create the Persistent class. 4.Create the mapping file for Persistent class. 5.Create the
Configuration file. 6.Create class that retrieves or stores the persistent object. 7.Run the
application. Create hibernate application using Annotations in Eclipse: The HB application can be
created with annotation. There are many annotations that can be used to create hibernate
application such as @Entity, @Table etc. Hibernate Annotations are based on the JPA 2
specification & supports all the features. All the JPA annotations are defined in
the javax.persistence package. Hibernate EntityManager implements the interfaces & life cycle
defined by the JPA specification.The core advantage of using hibernate annotation is that you
don't need to create mapping (hbm) file. Here, hibernate annotations are used to provide the
meta data. Steps: 1.Create the Maven project. 2.Add project info and config in pom.xml file.
3.Create the persistence class. 4.Create the config file. 5.Create the class that retrives or stores the
persistence object. 6. Run the application.
HIBERNATE LOGGING: Logging enables the programmer to write the log details into a file
permanently. Log4j & Logback frameworks can be used in hibernate framework to support logging.
There are 2 ways to perform logging using log4j: 1. By log4j.xml file: Steps to perform logging
using xml file- i. Load the log4j jar files with hibernate. ii. Create the log4j.xml file inside the src
folder (parallel with hibernate.cfg.xml file). 2. By log4j.properties file: Steps to perform logging
using properties file- i. Load the log4j jar files with hibernate. ii. Create the log4j.properties file
inside the src folder (parallel with hibernate.cfg.xml file).

HIBERNATE INHERITANCE MAPPING- We can map the inheritance hierarchy classes with the table
of the database. MAPPING STRATEGIES: 1.Table Per Hierarchy- In table per hierarchy mapping,
single table is required to map the whole hierarchy, an extra column is added to identify the class.
But nullable values are stored in the table. Example- There are three classes in this hierarchy.
Employee is the super class for Regular_Employee & Contract_Employee classes. Let's see the
mapping file for this hierarchy. In case of table per class hierarchy an discriminator column is
added by the hibernate framework that specifies the type of the record. It is mainly used to
distinguish the record. To specify this, discriminator subelement of class must be specified.
The subclass subelement of class, specifies the subclass. In this case, Regular_Employee &
Contract_Employee are subclasses of Employee class. Table structure for this hierarchy is as shown
below: 2.Table Per Concrete Class- In case of table per concrete class, tables are created as per
class. But duplicate column is added in subclass tables. Example-There will be three tables in the
database having no relations to each other. There are 2 ways to map the table with table per
concrete class strategy: i. By Union Subclass Element ii. By self creating the table for each class
Let's see how can we map this hierarchy by union-subclass element: In case of table per concrete
class, there will be 3 tables in database, each representing a particular class. The union-subclass
subelement of class, specifies subclass. It adds columns of parent table into this table. In other
words, it is working as a union. The table structure for each table will be as follow: 3.Table Per
Subclass- In case of Table Per Subclass, subclass mapped tables are related to parent class mapped
table by primary key & foreign key relationship. The <joined-subclass> element of class is used to
map the child class with parent using the primary key & foreign key relation. Example- In this
example, we are going to use hb2ddl.auto property to generate the table automatically. So we
don't need to be worried about creating tables in the database. Let's see how can we map this
hierarchy by joined-subclass element: In case of table per subclass class, there will be three tables
in the database, each representing a particular class. The joined-subclass subelement of class,
specifies the subclass. The key sub-element of joined-subclass is used to generate the foreign key
in the subclass mapped table. This foreign key will be associated with the primary key of parent
class mapped table. The table structure for each table will be as follows:
HIBERNATE LIFECYCLE- In Hibernate, either we create an object of an entity & save it into the
database, or we fetch the data of an entity from the database. Here, each entity is associated with
the lifecycle. The entity object passes through the different stages of the lifecycle. The Hibernate
lifecycle contains the following states: 1.Transient State- The transient state is the initial state of
an object. Once we create an instance of POJO class, then the object entered in the transient state.
Here, an object is not associated with the Session. So, the transient state is not related to any
database. Hence, modifications in the data don't affect any changes in the database. The transient
objects exist in the heap memory. They are independent of Hibernate. 2.Persistent State -As soon
as the object associated with the Session, it entered in the persistent state. Hence, we can say that
an object is in the persistence state when we save or persist it. Here, each object represents the
row of the database table. So, modifications in the data make changes in the database.
3.Detached State - Once we either close the session or clear its cache, then the object entered into
the detached state. As an object is no more associated with the Session, modifications in the data
don't affect any changes in the database. However, the detached object still has a representation
in the database. If we want to persist the changes made to a detached object, it is required to
reattach the application to a valid Hibernate session. To associate the detached object with the
new hibernate session, use any of these methods - load(), merge(), refresh(), update() or save() on
a new session with the reference of the detached object.

SPRING FRAMEWORK- Spring is a lightweight framework. It can be thought of as a framework of


frameworks because it provides support to various frameworks such as Struts, Hibernate,
Tapestry, EJB, JSF, etc. The framework, in broader sense, can be defined as a structure where we
find solution of the various technical problems. The Spring framework comprises several modules
such as IOC, AOP, DAO, ORM, etc. ADVANTAGES: Loose Coupling- The Spring applications are
loosely coupled because of dependency injection. Easy to Test- The Dependency Injection makes
easier to test the application. The EJB or Struts application require server to run the application but
Spring framework doesn't require server. Lightweight- Spring framework is lightweight because of
its POJO implementation. The Spring Framework doesn't force the programmer to inherit any class
or implement any interface. That is why it is said non-invasive. Fast Development- The
Dependency Injection feature of Spring Framework and it support to various frameworks makes
the easy development of JavaEE application. Powerful Abstraction- It provides powerful
abstraction to JavaEE specifications such as JMS, JDBC, JPA and JTA. APPLICATIONS: Here, we are
going to learn the simple steps to create the first spring application. To run this application, we are
not using any IDE. We are simply using the command prompt. Let's see the simple steps to create
the spring application- 1.create the class. 2. create the xml file to provide the values. 3. create the
test class. 4. Load the spring jar files. 5. Run the test class. SPRING MODULES- The Spring
framework comprises of many modules such as core, beans, context, expression language, AOP,
Aspects, Instrumentation, JDBC, ORM, OXM, JMS, Transaction, Web, Servlet, Struts etc. These
modules are grouped into Test, Core Container, AOP, Aspects, Instrumentation, Data Access /
Integration, Web (MVC / Remoting) as displayed in the following diagram. TEST- This layer
provides support of testing with JUnit and TestNG. SPRING CORE CONTAINER- The Spring Core
container contains core, beans, context and expression language (EL) modules. CORE & BEANS-
These modules provide IOC and Dependency Injection features. CONTEXT- This module supports
internationalization (I18N), EJB, JMS, Basic Remoting. EXPRESSION LANGUAGE- It is an extension
to the EL defined in JSP. It provides support to setting and getting property values, method
invocation, accessing collections and indexers, named variables, logical and arithmetic operators,
retrieval of objects by name etc. AOP, ASPECTS & INSTRUMENTATION- These modules support
aspect oriented programming implementation where you can use Advices, Pointcuts etc. to
decouple the code. The aspects module provides support to integration with Aspect. The
instrumentation module provides support to class instrumentation and classloader
implementations. DATA ACCESS/INTEGRATION- This group comprises of JDBC, ORM, OXM, JMS
and Transaction modules. These modules basically provide support to interact with the database.
WEB- This group comprises of Web, Web-Servlet, Web-Struts and Web-Portlet. These modules
provide support to create web application. SPRING WITH IDE: Spring in Myeclipse- Steps- 1.
create the java project. 2. add spring capabilities. 3. create the class. 4. create the xml file to
provide the values. 5. create the test class. Spring in Eclipse- Steps- 1. create the java project. 2.
add spring jar files. 3. create the class. 4. create the xml file to provide the values. 5. create the test
class. DEPENDENCY INJECTION: The Dependency Injection is a design pattern that removes the
dependency of the programs. In such case we provide the information from the external source
such as XML file. It makes our code loosely coupled and easier for testing. METHODS: Spring
framework provides two ways to inject dependency: By Constructor- We can inject the
dependency by constructor. The <constructor-arg> subelement of <bean> is used for
constructor injection. Here we are going to inject- primitive and String-based values; Dependent
object (contained object); Collection values etc. By Setter Method- We can inject the dependency
by setter method also. The <property> subelement of <bean> is used for setter injection. Here
we are going to inject- primitive and String-based values; Dependent object (contained object);
Collection values etc.

You might also like