Hibernate Flashcards

1
Q

What is ORM in Hibernate?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the advantages of Hibernate over JDBC?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some of the important interfaces of Hibernate framework?

A
1- Configuration
2- SessionFactory
3- Session
4- Criteria
5- Query
6- Transaction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Session in Hibernate?

A

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(),

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a SessionFactory?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you explain what is lazy loading in hibernate?

A

Lazy loading is mainly used for improving the application performance
by loading the objects on demand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What can you tell about Hibernate Configuration File?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can you explain the concept behind Hibernate Inheritance Mapping

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Hibernate Core Interfaces

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Transaction

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Query

A

is an interface defined in the org.hibernate package.

A Query instance is obtained by calling Session.createQuery()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Criteria API

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

XML vs Annotation configuration in Hibernate

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Hibernate Annotations

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Object States in Hibernate

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

HQL

A

Hibernate Query Language is the object-oriented query language of the Hibernate Framework.
HQL is very similar to SQL except that we query against persistent objects instead of tables and columns.
HQL is case-sensitive for properties like table and column names and not for keywords like SELECT, FROM, and was, etc.

17
Q

What is JPA?

A
A JPA (Java Persistence API) is a specification of Java which is used to access, manage, and persist data between Java object and relational database. 
It is considered as a standard approach for Object Relational Mapping.

JPA can be seen as a bridge between object-oriented domain models and relational database systems. Being a specification, JPA doesn’t perform any operation by itself. Thus, it requires implementation. So, ORM tools like Hibernate, TopLink, and iBatis implements JPA specifications for data persistence.

18
Q

What is Hibernate?

A

A Hibernate is a Java framework which is used to store the Java objects in the relational database system. It is an open-source, lightweight, ORM (Object Relational Mapping) tool.

Hibernate is an implementation of JPA. So, it follows the common standards provided by the JPA.

19
Q

@OneToMany(mappedBy = “author”, cascade = CascadeType.ALL, orphanRemoval = true)

@ManyToOne(fetch = FetchType.LAZY)

A

The mappedBy = “author” attribute tells us the book table is the owning side of the relationship.

The cascade = CascadeType.ALL tells Hibernate to propagate changes from any book to its related entities. For example, if we remove an author then all of its books will also be deleted from the database.

The orphanRemoval = true tells Hibernate to automatically remove orphaned entities. A book would become an orphan if it’s removed from the author’s list of books. This option tells Hibernate to automatically delete orphans.

The helper methods addBook() and removeBook() are useful in keeping the persisted context in sync. Strange things can happen when both sides of the bidirectional relationship aren’t updated. The database can get out of sync with the context and unexpected things can happen. These methods make it easy to update both sides of the relationship…

The fetch = FetchType.LAZY tells Hibernate to lazily load books for a given author. This means when we retrieve an author from the database, Hibernate won’t return the associated books for that author in the same call. A separate request is made to the database only when the application explicitly asks for author.getBooks().

20
Q

@joinColumn(name =Cart_id)

private Cart cart;

A

to specify the FK column with the PK

21
Q

Arrays class in Java

A

The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of Object class. The methods of this class can be used by the class name itself.