Hibernate Interview Questions and Answers
ANS: Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using (XML) configuration files. Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks.
2. What is Hibernate Query Language (HQL)?
ANS: Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL.
3. What is ORM?
ANS: ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.
4. Why do you need ORM tools like hibernate?
ANS: The main advantage of ORM like hibernate is that it shields developers from messy SQL. Apart from this, ORM provides following benefits:
High-level object-oriented API
Less Java code to write
No SQL to write
A lot less code to write
ORM framework generates database-specific SQL for you
5. What is a Hibernate Template?
ANS: Hibernate Template is a helper class that is used to simplify the data access code. This class supports automatically converts Hibernate Exceptions which is a checked exception into Data Access Exceptions which is an unchecked exception. Hibernate Template is typically used to implement data access or business logic services. The central method is execute (), that supports the Hibernate code that implements Hibernate Callback interface.
6. What are the benefits does Hibernate Template provide?
ANS: The benefits of Hibernate Template are:
* Hibernate Template, a Spring Template class simplifies interactions with Hibernate Session.
* Common functions are simplified to single method calls.
* Sessions are automatically closed.
* Exceptions are automatically caught and converted to runtime exceptions.
7. What Does Hibernate Simplify?
ANS: Hibernate simplifies:
Saving and retrieving your domain objects
Making database column and table name changes
Centralizing pre save and post retrieve logic
Complex joins for retrieving related items
Schema creation from object model
8. What is the advantage of Hibernate over jdbc?
ANS: Hibernate is flexible and powerful ORM solution to map Java classes to database tables. Hibernate itself takes care of this mapping using XML files so developer does not need to write code for this.
With JDBC, developer has to write code to map an object model’s data representation to a relational data model and its corresponding database schema.
With JDBC, caching is maintained by hand-coding.
Hibernate, with Transparent Persistence, cache is set to application work space. Automatic Transparent Persistence allows the developer to concentrate more on business logic rather than this application code.
9. What is Hibernate proxy?
ANS: The proxy attribute enables lazy initialization of persistent instances of the class. Hibernate will initially return CGLIB proxies which implement the named interface. The actual persistent object will be loaded when a method of the proxy is invoked.
10. What are the types of Hibernate instance states ?
ANS: Three types of instance states:
Transient -The instance is not associated with any persistence context
Persistent -The instance is associated with a persistence context
Detached -The instance was associated with a persistence context which has been closed – currently not associated.
11. What are the most common methods of Hibernate configuration?
ANS: The most common methods of Hibernate configuration are:
XML configuration (hibernate.cfg.xml)
12. How will you configure Hibernate?
ANS: The configuration files hibernate.cfg.xml (or hibernate. properties) and mapping files *.hbm.xml are used by the Configuration class to create (i.e. configure and bootstrap hibernate) the Session Factory, which in turn creates the Session instances. Session instances are the primary interface for the persistence service.
13. What is a Session Factory? Is it a thread-safe object?
ANS: SessionFactory is Hibernate’s concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in an application code.
SessionFactory session Factory = new Configuration( ).configure( ).buildSessionfactory( );
14. What are the benefits of detached objects?
ANS: When long transactions are required due to user think-time, it is the best practice to break the long transaction up into two or more transactions. You can use detached objects from the first transaction to carry data all the way up to the presentation layer. These detached objects get modified outside a transaction and later on re-attached to a new transaction via another session.
15. What are the Core interfaces are of Hibernate framework?
ANS: The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.
Query and Criteria interfaces
16. What is the general flow of Hibernate communication with RDBMS?
ANS: The general flow of Hibernate communication with RDBMS is :
* Load the Hibernate configuration file and create configuration object. * It will automatically load all hbm mapping files
* Create session factory from configuration object
* Get one session from this session factory
* Create HQL Query
* Execute query to get list containing Java objects
17. What is component mapping in Hibernate?
ANS: * A component is an object saved as a value, not as a reference
* A component can be saved directly without needing to declare interfaces or identifier properties.
* Required to define an empty constructor.
* Shared references not supported.
18. Explain the difference between hibernate and Spring?
ANS: Hibernate provides a lot of feasibility while dealing with the database.
It provides with ORM where in you are able to perform database transaction is the form of objects mapped to corresponding table, using configuration xml files.
Spring, on the other hand is a framework that helps you follow the MVC architecture in an effective and efficient way. It simplifies writing back end code as well as front end stuff providing clear demarcation simultaneously.
Tags: free sample interview questions, hibernate interview, hibernate interview question, hibernate interview questions and answers for experienced, hibernate questions, hibernate spring interview questions, inter questions and answers, interview questions for interview, interview questions hibernate, interview questions on hibernate