Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Is hibernate prone to SQL injection attack?

Answer»

SQL INJECTION attack is a serious vulnerability in terms of web security wherein an attacker can interfere with the queries made by an application/website to its DATABASE thereby allowing the attacker to view sensitive data which are generally irretrievable. It can also give the attacker to modify/ remove the data RESULTING in damages to the application behavior.

Hibernate does not provide immunity to SQL Injection. However, following good practices avoids SQL injection attacks. It is always advisable to follow any of the below options:

  • Incorporate Prepared STATEMENTS that USE Parameterized Queries.
  • Use Stored Procedures.
  • Ensure data sanity by doing input validation.
2.

Can you explain the concept behind Hibernate Inheritance Mapping?

Answer»

Java is an Object-Oriented Programming Language and INHERITANCE is one of the most important pillars of object-oriented principles. To represent any models in Java, inheritance is most commonly used to simplify and simplify the relationship. But, there is a catch. Relational databases do not SUPPORT inheritance. They have a flat structure.

Hibernate’s Inheritance Mapping strategies deal with solving how to hibernate being an ORM tries to map this problem between the inheritance of Java and flat structure of Databases.

Consider the example where we have to divide InterviewBitEmployee into CONTRACT and Permanent Employees represented by IBContractEmployee and IBPermanentEmployee classes respectively. Now the task of hibernate is to represent these 2 employee types by CONSIDERING the below restrictions:

The general employee details are defined in the parent InterviewBitEmployee class. 

Contract and Permanent employee-specific details are stored in IBContractEmployee and IBPermanentEmployee classes respectively

The class diagram of this system is as shown below:

Hibernate’s Inheritance Mapping

 

There are different inheritance mapping strategies available:

  • Single Table STRATEGY
  • Table Per Class Strategy
  • Mapped Super Class Strategy
  • Joined Table Strategy
3.

How do you create an immutable class in hibernate?

Answer»

Immutable class in hibernate creation could be in the following way. If we are using the XML FORM of configuration, then a class can be made immutable by markingmutable=false. The default value is TRUE there which indicating that the class was not created by default.

In the CASE of using annotations, immutable classes in hibernate can also be created by using @Immutable ANNOTATION.

4.

What can you tell about Hibernate Configuration File?

Answer»

Hibernate Configuration File or 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.
Hibernate facilitates PROVIDING the configuration either in an XML file (like hibernate.cfg.xml) or a properties file (like hibernate.properties).

This file is used to define the below information:

  • Database CONNECTION details: Driver class, URL, username, and password.
  • There MUST be one configuration file for each database used in the application, suppose if we want to connect with 2 databases, then we must create 2 configuration files with different names.
  • Hibernate properties: Dialect, show_sql, second_level_cache, and mapping file names.
5.

What is the difference between first level cache and second level cache?

Answer»

Hibernate has 2 cache types. First level and second level cache for which the difference is given below:

First Level CacheSecond Level Cache
This is local to the Session object and cannot be shared between multiple SESSIONS.This cache is maintained at the SessionFactory level and shared among all sessions in Hibernate.
This cache is enabled by default and there is no way to disable it.This is disabled by default, but we can enable it through configuration.
The first level cache is AVAILABLE only until the session is OPEN, once the session is closed, the first level cache is destroyed.The second-level cache is available through the application’s life cycle, it is only destroyed and recreated when an application is restarted.

If an entity or object is loaded by CALLING the get() method then Hibernate first checked the first level cache, if it doesn’t find the object then it goes to the second level cache if CONFIGURED. If the object is not found then it finally goes to the database and returns the object, if there is no corresponding row in the table then it returns null.

6.

Can you explain what is lazy loading in hibernate?

Answer»

Lazy LOADING is mainly used for IMPROVING the application performance by HELPING to load the child objects on demand.

It is to be noted that, since Hibernate 3 version, this feature has been ENABLED by default. This signifies that child objects are not loaded until the parent gets loaded.

7.

What do you think about the statement - “session being a thread-safe object”?

Answer»

No, Session is not a thread-safe object which means that any NUMBER of threads can access DATA from it SIMULTANEOUSLY.

8.

What is a SessionFactory?

Answer»

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.
As a good practice, the application generally has a single instance of SessionFactory. The internal STATE of a SessionFactory which includes metadata about ORM is immutable, i.e once the instance is created, it cannot be changed.

This also provides the facility to get information LIKE statistics and metadata related to a class, query executions, etc. It also holds second-level cache data if ENABLED.

9.

What is a Session in Hibernate?

Answer»

A SESSION is an OBJECT that maintains the connection between Java object application and database. Session also has METHODS for storing, retrieving, modifying or deleting data from database using methods like persist(), load(), get(), UPDATE(), delete(), etc. Additionally, It has factory methods to return Query, Criteria, and Transaction objects.

10.

What are some of the important interfaces of Hibernate framework?

Answer»

HIBERNATE CORE INTERFACES are:

  • Configuration
  • SessionFactory
  • Session
  • Criteria
  • Query
  • Transaction
11.

What are the advantages of Hibernate over JDBC?

Answer»

- The ADVANTAGES of Hibernate over JDBC are listed below:

  • Clean Readable CODE: Using hibernate, helps in eliminating a lot of JDBC API-based boiler-plate codes, thereby making the code look cleaner and readable.
  • HQL (Hibernate Query Language): Hibernate provides HQL which is closer to Java and is object-oriented in nature. This helps in reducing the BURDEN on developers for writing database independent queries. In JDBC, this is not the CASE. A developer has to know the database-specific codes.
  • Transaction Management: JDBC doesn't support implicit transaction management. It is upon the developer to write transaction management code using commit and rollback methods. Whereas, Hibernate IMPLICITY provides this feature.
  • Exception Handling: Hibernate wraps the JDBC exceptions and throws unchecked exceptions like JDBCException or HibernateException. This along with the built-in transaction management system helps developers to avoid writing multiple try-catch blocks to handle exceptions. In the case of JDBC, it throws a checked exception called SQLException thereby mandating the developer to write try-catch blocks to handle this exception at compile time.
  • Special Features: Hibernate supports OOPs features like inheritance, associations and also supports collections. These are not available in JDBC.
12.

What is ORM in Hibernate?

Answer»

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 constructs. This tool also helps GREATLY in simplifying data retrieval, CREATION, and manipulation.

Object Relational Mapping