Hibernate Flashcards
What is ORM in Hibernate?
ORM stands for (Object Relational Mapping).
This is a mapping tool pattern
mainly used for converting data stored in a relational database to an object used in object-oriented programming.
This tool also helps greatly in simplifying data retrieval, creation, and manipulation.
What are the advantages of Hibernate over JDBC?
1- Clean Readable Code
2- HQL which is closer to Java and is object-oriented in nature.
3- Transaction Management: Hibernate implicity provides this feature
4- Exception Handling:
Hibernate wraps the JDBC exceptions and throws unchecked exceptions
so it avoid writing multiple try-catch blocks to handle exceptions.
5- Special Features
Hibernate supports OOPs features like inheritance, associations and also supports collections
What are some of the important interfaces of Hibernate framework?
1- Configuration 2- SessionFactory 3- Session 4- Criteria 5- Query 6- Transaction
What is a Session in Hibernate?
A session is an object that maintains the connection between Java object application and database.
It has methods for storing, retrieving, modifying or deleting data from database using methods like persist(), load(), get(), update(), delete(),
What is a SessionFactory?
SessionFactory provides an instance of Session. It is a factory class that gives the Session objects based on the configuration parameters in order to establish the connection to the database.
Can you explain what is lazy loading in hibernate?
Lazy loading is mainly used for improving the application performance
by loading the objects on demand.
What can you tell about Hibernate Configuration File?
Hibernate.cfg.xml is one of the most required configuration files in Hibernate.
By default, this file is placed under the src/main/resource folder.
The file contains database related configurations and session-related configurations.
define the below information:
1- Database connection details: Driver class, URL, username, and password.
2-Hibernate properties
Can you explain the concept behind Hibernate Inheritance Mapping
Java is an Object-Oriented Programming Language and Inheritance is one of the most important But, there are some Relational databases do not support inheritance
Hibernate’s Inheritance Mapping strategies deal with solving this problem between the inheritance of Java and flat structure of Databases.
Hibernate Core Interfaces
The Hibernate core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.
Transaction
Is an interface defined in the org.hibernate package.
A transaction is associated with a Session and usually instantiated by a call to Session.beginTransaction().
Query
is an interface defined in the org.hibernate package.
A Query instance is obtained by calling Session.createQuery()
Criteria API
is an interface defined in the org.hibernate package.
Criteria are a programmatic and type safe way to fetch data from the relational database.
we can pull the data from the database using session methods; HQL, JPQL, Native SQL, or the Criteria API.
XML vs Annotation configuration in Hibernate
Hibernate classically uses an XML mapping file for the transformation of data from POJO Classes to database tables, and vice versa.
An XML file gives the ability to change the configuration without rebuilding the whole project.
Hibernate Annotations
Hibernate Annotations are used to provide metadata configuration inside the POJO class, so we can understand the table structure and POJO class simultaneously.
Hibernate Annotations are based on the JPA 2 specification. All the JPA annotations are defined in the javax.persistence package. Some of the annotation used for table and column mappings are listed below:
@Entity @Table @Id @GeneratedValue @Column
Object States in Hibernate
Transient State: When an object is created using the new operator and not yet associated with a Hibernate Session
Persistent State : The object state is persistent when it is associated with the hibernate session.
Detached State: When a persistent object has its session closed, then it becomes detached.