Hibernate Flashcards
What is Hibernate framework?
Hibernate is a popular Object Relational Mapping (ORM)
framework of Java. It helps in mapping the Object Oriented Domain
model to Relational Database tables.
Hibernate is a free software distributed under GNU license.
Hibernate also provides implementation of Java Persistence API
(JPA).
In simple words, it is a framework to retrieve and store data from
database tables from Java.
What is an Object Relational Mapping (ORM)?
Object Relational Mapping (ORM) is a programming technique to
map data from a relational database to Object oriented domain
model. This is the core of Hibernate framework.
In case of Java, most of the software is based on OOPS design. But
the data stored in Database is based on Relation Database
Management System (RDBMS).
ORM helps in data retrieval in an Object Oriented way from an
RDBMS. It reduces the effort of developers in writing queries to
access and insert data
What is the purpose of Configuration Interface in
Hibernate?
Configuration interface can be implemented in an application to specify the properties and mapping documents for creating a SessionFactory in Hibernate.
By default, a new instance of Configuration uses properties mentioned in hibernate.properties file.
Configuration is mainly an initialization time object that loads the
properties in helps in creating SessionFactory with these properties.
In short, Configuration interface is used for configuring Hibernate
framework in an application.
hat is Object Relational Impedance Mismatch?
Object Relational Impedance Mismatch (ORIM) is also known as paradigm mismatch. It means that Object model and Relational
model do not work well with each other.
Relational model or a RDBMS represents data in tabular format like a spreadsheet. Object model or OOPS represents the data as an inter-connected graph of objects.
Mixing these two models leads to various problems. The common name for these issues is Object Relational Impedance Mismatch.
What are the key characteristics of Hibernate?
Object/Relational Mapping (ORM): Hibernate provides ORM
capabilities to developers. So then can write code in Object model
for connecting with data in Relational model.
JPA Provider: Hibernate provides an excellent implementation of
Java Persistence API (JPA) specification.
Idiomatic persistence: Hibernate provides persistence based on
natural Object-oriented idioms with full support for inheritance,
polymorphism, association, composition, and the Java collections
framework. It can work with any data for persistence.
High Performance: Hibernate provides high level of performance
supporting features like- lazy initialization, multiple fetching
strategies, optimistic locking etc. Hibernate does not need its own
database tables or fields. It can generate SQL at system
initialization to provide better performance at runtime.
Scalability: Hibernate works well in multi server clusters. It has
built in scalability support. It can work well for small projects as
well as for large business software.
Reliable: Hibernate very reliable and stable framework. This is the
reason for its worldwide acceptance and popularity among
developer community.
Extensible: Hibernate is quite generic in nature. It can be configured
and extended as per the use case of application.
Can you tell us about the core interfaces of Hibernate framework?
Configuration: Configuration interface can be implemented in an
application to specify the properties and mapping documents for
creating a SessionFactory in Hibernate. Hibernate application
bootstraps by using this interface.
SessionFactory: In Hibernate, SessionFactory is used to create and
manage Sessions. Generally, there is one SessionFactory created for
one database. It is a thread-safe interface that works well in multithreaded applications.
Session: Session is a lightweight object that is used at runtime
between a Java application and Hibernate. It contains methods to
create, read and delete operations for entity classes. It is a basic
class that abstracts the concept of persistence.
Transaction: This is an optional interface. It is a short lived object
that is used for encapsulating the overall work based on unit of
work design pattern. A Session can have multiple Transactions.
Query: This interface encapsulates the behavior of an objectoriented query in Hibernate. It can accept parameters and execute
the queries to fetch results. Same query can be executed multiple
times.
Criteria: This is a simplified API to retrieve objects by creating
Criterion objects. It is very easy to use for creating Search like
features.
How will you map the columns of a DB table to the properties of a Java class in Hibernate?
We can map the class properties and table columns by using one of
the two ways:
XML: We can map the column of a table to the property of a class in
XMLfile. It is generally with extension hbm.xml
Annotation: We can also use annotations @Entity and @Table to
map a column to the property of a class.
What are the steps for creating a SessionFactory in Hibernate?
Steps to create a SessionFactory in Hibernate are:
Configuration: First create a Configuration object. This will refer to
the path of configuration file.
Resource: Add config file resource to Configuration object.
Properties: Set properties in the Configuration object.
SessionFactory: Use Configuration object to build SessionFactory.
Egg.
Configuration config = new Configuration();
config.addResource(“testInstance/configuration.hbm.xml”);
config.setProperties( System.getProperties() );
SessionFactory sessions = config.buildSessionFactory();
Why do we use POJO in Hibernate?
POJO stands for Plain Old Java Objects. A POJO is java bean with
getter and setter methods for each property of the bean.
It is a simple class that encapsulates an object’s properties and
provides access through setters and getters.
Some of the reasons for using POJO in Hibernate are:
POJO emphasizes the fact that this class is a simple Java class, not
a heavy class like EJB.
POJO is a well-constructed class, so it works well with Hibernate
proxies.
POJO also comes with a default constructor that makes it easier to
persist with a default constructor.
What is Hibernate Query Language (HQL)?
Hibernate Query Language is also known as HQL. It is an Object
Oriented language. But it is similar to SQL.
HQL works well with persistent objects and their properties. HQL
does not work on database tables.
HQL queries are translated into native SQL queries specific to a
database.
HQL supports direct running of native SQL queries also. But it
creates an issue in Database portability
What is Criteria API in Hibernate?
Criteria is a simplified API in Hibernate to get entities from database by creating Criterion objects.
It is a very intuitive and convenient approach for search features.
Users can specify different criteria for searching entities and Criteria API can handle these.
Criterion instances are obtained through factory methods on Restrictions.
What are the different types of
collections supported by Hibernate?
Indexed Collections: List and Maps
Sorted Collections: java.util.SortedMap and java.util.SortedSet
What is the difference between
session.save() and
session.saveOrUpdate() methods in
Hibernate?
Save method first stores an object in the database. Then it persists
the given transient instance by assigning a generated identifier.
Finally, it returns the id of the entity that is just created.
SaveOrUpdate() method calls either save() or update() method. It
selects one of these methods based on the existence of identifier.
If an identifier exists for the entity then update() method is called. If
there is no identifier for the entity then save() method is called as
mentioned earlier.
What are the advantages of
Hibernate framework over JDBC?
Database Portability: Hibernate can be used with multiple types of
database with easy portability. In JDBC, developer has to write
database specific native queries. These native queries can reduce
the database portability of the code.
Connection Pool: Hibernate handles connection pooling very well.
JDBC requires connection pooling to be defined by developer.
Complexity: Hibernate handles complex query scenarios very well
with its internal API like Criteria. So developer need not gain
expertise in writing complex SQL queries. In JDBC application
developer writes most of the queries.
What is the Transient state of an object in Hibernate?
When an object is just instantiated using the new operator but is not
associated with a Hibernate Session, then the object is in Transient
state.
In Transient state, object does not have a persistent representation
in database. Also there is no identifier assigned to an object in
Transient state.
An object in Transient state can be garbage collected if there is no
reference pointing to it