What is API in Java
An API stands for Application Programming Interface. An API in Java is a collection of
prewritten packages, classes, and interfaces with their respective methods, fields,
and constructors. Sometimes, it is required for a programmer to use certain
technologies without much concern about internal implementations.
API is useful in these situations. They make it easier for the developers to build
applications by using predefined operations in APIs. There is more than 4500 APIs
available in Java.
One example of Java API is REST API. It is a web standard architecture and uses the
HTTP protocol for data communication.
Overall, API supports the development process. Furthermore, API will reduce the
code length and improve the code reusability. They also help to access remote
resources and are used for communication between services.
What is Framework in Java
Java framework is a collection of classes of predefined code that allows the
developers to add them to their own programs to solve a problem. It provides the
required functionalities to build and deploy an application.
A Java framework provides functionalities as a part of a larger software platform. It
can consist of support programs, compilers, code libraries, toolsets and APIs that
support the development of the entire project.
Framework is different from a usual library due to a number of reasons.
1. Firstly, it provides inversion of control. This means the flow of the program is
controlled by the framework.
2. Secondly, a Java framework is extensible. Therefore, the programmer can
extend the framework by overriding the methods or by adding specialized
code that performs specific functionalities.
3. Thirdly, there is a non-modifiable framework code. Therefore, programmers
can extend the framework without changing the code. Overall, a Java
4. Framework provides multiple advantages. It increases efficiency and
makes the application more secure.
Difference between API and Framework in Java
API Framework
API in Java is a set of subroutine Framework in Java is an abstraction in
definitions, communication protocols, which software providing generic
and tools for building software. functionality can be selectively changed
by additional user-written code, thus
providing application specific software
API works as an interface between Java Framework is used to design
applications applications such as MVC web
applications. They provide the model to
develop the application.
Examples
REST is an example for a Java API. Spring is an example for a Java framework.
Hibernate
Hibernate is a framework which provides some abstraction layer means
programmer don’t have to worry about the implementations, Hibernate do
implementations for you internally like Establishing a connection with the database,
writing query to perform CRUD operations etc.
It is a java framework which is used to develop persistence logic.
Persistence logic means to store and process the data for long
use.
Hibernate is a open source, non-invasive, light-weight java ORM(Object
relational mapping) framework to develop objects which is independent of the
database software and make independent persistence logic in all JAVA, JEE.
Hibernate invented by Gavin King in 2001. He also invented JBoss server and JPA.
Non-invasive means:
The classes of Hibernate application development are loosely coupled
classes with respect to Hibernate API i.e. Hibernate class need not to
implement hibernate API interfaces and need not to extend from Hibernate
API classes.
Functionalities supported by Hibernate framework
1. Hibernate framework support Auto DDL operations. In JDBC manually we
have to create table and declare the data-type for each and every column. But
Hibernate can do DDL operations for you internally like creation of table,
drop a table, alter a table etc.
2. Hibernate supports Auto Primary key generation. It means in JDBC we have to
manually set a primary key for a table. But Hibernate can this task for you.
3. Hibernate framework is independent of Database because it supports HQL
(Hibernate Query Language) which is not specific to any database,
whereas JDBC is database dependent.
4. In Hibernate, Exception Handling is not mandatory, whereas In JDBC
exception handling is mandatory.
5. Hibernate supports Cache Memory whereas JDBC does not support cache
memory.
6. Hibernate is a ORM tool means it support Object relational mapping.
Whereas JDBC is not object oriented moreover we are dealing with values
means primitive data. In hibernate each record is represented as a Object but
in JDBC each record is nothing but a data which is nothing but primitive
values.
7. Hibernate supports Inheritance, Associations, Collections
8. Hibernate supports a special query language(HQL) which is Database
vendor independent.
9. Hibernate supports annotations, apart from XML.
What is ORM?
object/relational mapping is the automated (and transparent) persistence
of objects in a Java application to the tables in a relational database, using
metadata that describes the mapping between the objects and the database.
ORM, in essence, works by (reversibly) transforming data from one
representation to another.
ORM is also called as object role modeling/object relational mapping.
Java ORM Frameworks:
There are several persistent frameworks and ORM options in Java. A persistent
framework is an ORM service that stores and retrieves objects into a relational
database.
1. Enterprise JavaBeans Entity Beans
2. Java Data Objects
3. Top Link
4. Spring DAO
5. Hibernate
6. And many more
What are the Simple Hibernate Application
Requirements?
1 Entity class/POJO class
2. Mapping file(Required if you are not using annotations)
3. Configuration file(hibernate.cfg.xml
4. DAO class (Where we write our logic to work with database)
What are the Steps to develop hibernate applications? -
Step 1: Develop persistent/domain/entity/pojo class for each table of the
relational model
Step 2: For each entity develop a mapping file or Hibernate
Annotation
Step 3: Develop the configuration file(hibernate.cfg.xml)
Step 4: Add hibernate framework jar files i n the classpath
Step 5: Make use of hibernate API and perform persistent operations
How to Make use of hibernate API to perform persistent'
operations?
STEP1:
Create Configuration object
Configuration configuration = new
Configuration();
STEP2:
Read configuration file along with mapping files using configure()
rnethod of Configuration Object
configuration.configure(
);
STEP3:
Build a SessionFactory from Configuration
SessionFactory factory = configuration.bhildSessionFactory();
ST P4:
Get Session from SessionFactory
object Session session =
factory.openSession();
STEP 5:
BEGIN Transacton
Transaction
transaction=session.beginTransaction();
transaction.begin();
STEP6:
Perform persistence
operations
Save/delete/read/updat
e
STEP7: commit transaction and close session.
What is hibernate configuration file?
It is an XML file in which database connection details (username, password,
url, driver class name) and ,Hibernate Properties(dialect, show-sql, second-
level-cache ... etc) and Mapping file name(s) are specified to the hibernate
Hibernate uses this file to establish connection to the particular database
server . Standard for this file is <hibernate.cfg.xml>
We must create one configuration file for each database we are going to
use, suppose if we want to connect wi th 2 databases, like Oracle, MySql,
then we must create 2 configuration files.
No. of databases we are using = That many number of
configuration files We can write this configuration in 2 ways ...
1. XML file
2. Properties file(old style)
We don't have annotations to write configuration details. Actually in hibernate
1.x,
2.x we defined this configuration by using .properties file, but from 3.x
XML came into picture.
XmL files are always recommended to use
Hibernate Architecture:
The above diagram as follows :
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();
example 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();