Hibernate Interview Questions and Answers, Hibernate Interview Questions and Answers Freshers, Hibernate Interview Questions and Answers, Hibernate Interview Questions
Before getting on to the Hibernate interview questions, the student must know that the Hibernate is a continuously varying field which needs the students as well as professionals to upgrade their skills with the new features and knowledge, to get fit for the jobs associated with Hibernate. This post related to Hibernate Interview Questions and Answers, Hibernate Interview Questions and Answers Freshers, Hibernate Interview Questions and Answers, Hibernate Interview Questions will help you let out find all the solutions that are frequently asked in you upcoming Hibernate interview.
Over thousands of vacancies available for the Hibernate developers, experts must be acquaintance with all the component of Hibernate technologies. This is necessary for the students in order to have in-depth knowledge of the subject so that they can have best employment opportunities in the future. Knowing every little detail about Hibernate is the best approach to solve the problems linked with problem.
APTRON has spent hours and hours in researching about the Hibernate Interview Questions and Answers, Hibernate Interview Questions and Answers Freshers, Hibernate Interview Questions and Answers, Hibernate Interview Questions that you might encounter in your upcoming interview. All these questions will alone help you to crack the interview and make you the best among all your competitors.
First of all, let us tell you about how the Hibernate technology is evolving in today’s world and how demanding it is in the upcoming years. In fact, according to one study, most of the companies and businesses have moved to the Hibernate. Now, you cannot predict how huge the future is going to be for the people experienced in the related technologies.
Hence, if you are looking for boosting up your profile and securing your future, Hibernate will help you in reaching the zenith of your career. Apart from this, you would also have a lot of opportunities as a fresher.
These questions alone are omnipotent. Read and re-read the questions and their solutions to get accustomed to what you will be asked in the interview. These Hibernate interview questions and answers will also help you on your way to mastering the skills and will take you to the giant world where worldwide and local businesses, huge or medium, are picking up the best and quality Hibernate professionals.
This ultimate list of best Hibernate interview questions will ride you through the quick knowledge of the subject and topics like Hibernate distribution, Configuring Hibernate, Session Factory configuration, Configuration class. This Hibernate interview questions and answers can be your next gateway to your next job as a Hibernate expert.
These are very Basic Hibernate Interview Questions and Answers for freshers and experienced both.
Q1: What is Hibernate Framework?
A1: Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is java based ORM tool that provides framework for mapping application domain objects to the relational database tables and vice versa.
Hibernate provides reference implementation of Java Persistence API, that makes it a great choice as ORM tool with benefits of loose coupling. We can use Hibernate persistence API for CRUD operations. Hibernate framework provide option to map plain old java objects to traditional database tables with the use of JPA annotations as well as XML based configuration.
Similarly hibernate configurations are flexible and can be done from XML configuration file as well as programmatically.
Q2: What is Java Persistence API (JPA)?
A2: Java Persistence API (JPA) provides specification for managing the relational data in applications. Current JPA version 2.1 was started in July 2011 as JSR 338. JPA 2.1 was approved as final on 22 May 2013.
JPA specifications is defined with annotations in javax.persistence package. Using JPA annotation helps us in writing implementation independent code.
Q3: Name some important interfaces of Hibernate framework?
A3: Some of the important interfaces of Hibernate framework are:
- SessionFactory (org.hibernate.SessionFactory): SessionFactory is an immutable thread-safe cache of compiled mappings for a single database. We need to initialize SessionFactory once and then we can cache and reuse it. SessionFactory instance is used to get the Session objects for database operations.
- Session (org.hibernate.Session): Session is a single-threaded, short-lived object representing a conversation between the application and the persistent store. It wraps JDBC java.sql.Connection and works as a factory for org.hibernate.Transaction. We should open session only when it’s required and close it as soon as we are done using it. Session object is the interface between java application code and hibernate framework and provide methods for CRUD operations.
- Transaction (org.hibernate.Transaction): Transaction is a single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC or JTA transaction. A org.hibernate.Session might span multiple org.hibernate.Transaction in some cases.
Q4: What is hibernate mapping file?
A4: Hibernate mapping file is used to define the entity bean fields and database table column mappings. We know that JPA annotations can be used for mapping but sometimes XML mapping file comes handy when we are using third party classes and we can’t use annotations.
Q5: Hibernate SessionFactory is thread safe?
A5: Internal state of SessionFactory is immutable, so it’s thread safe. Multiple threads can access it simultaneously to get Session instances.
Q6: What is difference between Hibernate Session get() and load() method?
A6: Hibernate session comes with different methods to load data from database. get and load are most used methods, at first look they seems similar but there are some differences between them.
- get() loads the data as soon as it’s called whereas load() returns a proxy object and loads data only when it’s actually required, so load() is better because it support lazy loading.
- Since load() throws exception when data is not found, we should use it only when we know data exists.
- We should use get() when we want to make sure data exists in the database.
For clarification regarding the differences, please read Hibernate get vs load.
Q7: What’s Hibernate?
A7: Hibernate is a popular framework of Java which allows an efficient Object Relational mapping using configuration files in XML format. After java objects mapping to database tables, database is used and handled using Java objects without writing complex database queries.
Q8: What is ORM?
A8: ORM (Object Relational Mapping) is the fundamental concept of Hibernate framework which maps database tables with Java Objects and then provides various API’s to perform different types of operations on the data tables.
Q9: What’s HQL?
A9: HQL is the query language used in Hibernate which is an extension of SQL. HQL is very efficient, simple and flexible query language to do various type of operations on relational database without writing complex database queries.
Q10: What are the two types of collections in hibernate?
A10: Following are the two types of collections in hibernate:
a. Sorted Collection
b. Order Collection
Q11: How can we get hibernate statistics?
A11: We can get hibernate statistics using getStatistics() method of SessionFactory class as shown below:
Q12: When an instance goes in detached state in hibernate?
A12: When an instance was earlier associated with some persistent context (e.g. a table) and is no longer associated, it’s called to be in detached state.
Q13: What the four ORM levels are in hibernate?
A13: Following are the four ORM levels in hibernate:
a. Pure Relational
b. Light Object Mapping
c. Medium Object Mapping
d. Full Object Mapping
Q14: What is SessionFactory?
A14: SessionFactory provides the instance of Session. It is a factory of Session. It holds the data of second level cache that is not enabled by default.
Q15: What is Session?
A15: It maintains a connection between hibernate application and database.
It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.
It is a factory of Query, Criteria and Transaction i.e. it provides factory methods to return these instances.
Q16: What is the difference between get and load method?
A16: The differences between get() and load() methods are given below.
|1)||Returns null if object is not found.||Throws ObjectNotFoundException if object is not found.|
|2)||get() method always hit the database.||load() method doesn’t hit the database.|
|3)||It returns real object not proxy.||It returns proxy object.|
|4)||It should be used if you are not sure about the existence of instance.||It should be used if you are sure that instance exists.|
Q17: What is the difference between session.save() and session.persist() method?
|1)||returns the identifier (Serializable) of the instance.||return nothing because its return type is void.|
|2)||Syn: public Serializable save(Object o)||Syn: public void persist(Object o)|
Q18: What are the states of object in hibernate?
A18: There are 3 states of object (instance) in hibernate.
- Transient: The object is in transient state if it is just created but has no primary key (identifier) and not associated with session.
- Persistent: The object is in persistent state if session is open, and you just saved the instance in the database or retrieved the instance from the database.
- Detached: The object is in detached state if session is closed. After detached state, object comes to persistent state if you call lock() or update() method.
Q19: How to make a immutable class in hibernate?
A19: If you mark a class as mutable=”false”, class will be treated as an immutable class. By default, it is mutable=”true”.
Q20: What is lazy loading in hibernate?
A20: Lazy loading in hibernate improves the performance. It loads the child objects on demand.
Since Hibernate 3, lazy loading is enabled by default, you don’t need to do lazy=”true”. It means not to load the child objects when parent is loaded.
Q21: What is the difference between first level cache and second level cache?
|No.||First Level Cache||Second Level Cache|
|1)||First Level Cache is associated with Session.||Second Level Cache is associated with SessionFactory.|
|2)||It is enabled by default.||It is not enabled by default.|
Q22: What are the two key components of a hibernate configuration object?
A22: The Configuration object provides two keys components –
- Database Connection – This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.
- Class Mapping Setup
This component creates the connection between the Java classes and database tables.
Q23: What is Criteria in hibernate?
A23: Criteria object are used to create and execute object oriented criteria queries to retrieve objects.
Q24: Which method is used to remove a persistent instance from the datastore?
A24: Session.delete removes a persistent instance from the datastore.
Q25: What are the best practices that hibernate recommends for persistent classes.
A25: There are following main rules of persistent classes, however, none of these rules are hard requirements.
- All Java classes that will be persisted need a default constructor.
- All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database. This property maps to the primary key column of a database table.
- All attributes that will be persisted should be declared private and have getXXX and setXXX methods defined in the JavaBean style.
- A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.
- All classes that do not extend or implement some specialized classes and interfaces required by the EJB framework.
Q26: Which element of hbm.xml is used to map a java.util.Set property in hibernate?
A26: This is mapped with a element and initialized with java.util.HashSet.
Q27: What is the difference between save() and persist() methods of session object?
A27: session.save saves the object and returns the id of the instance whereas persist do not return anything after saving the instance.
Q28: What is N+1 SELECT problem in Hibernate?
A28: The N+1 SELECT problem is a result of lazy loading and load on demand fetching strategy. In this case, Hibernate ends up executing N+1 SQL queries to populate a collection of N elements. For example, if you have a List of N Items where each Item has a dependency on a collection of Bid object. Now if you want to find the highest bid for each item then Hibernate will fire 1 query to load all items and N subsequent queries to load Bid for each item. So in order to find the highest bid for each item your application end up firing N+1 queries. It’s one of the important Hibernate interview questions and I suggest to read chapter 13 of Java Persistence with Hibernate to understand this problem in more details.
Q29: What are different types of caches available in Hibernate?
A29: This is another common Hibernate interview question. Hibernate provides the out-of-box caching solution but there are many caches e.g. first level cache, second level cache and query cache. First level cache is maintained at Session level and cannot be disabled but the second level cache is required to be configured with external cache provider like EhCache.
Q30: What is different between Session and Sessionfactory in Hibernate? (detailed answer)
A30: This is another popular Hibernate interview question, mostly at a telephonic round of interviews. The main difference between Session and SessionFactory is that former is a single-threaded, short-lived object while later is Immutable and shared by all Session. It also lives until the Hibernate is running. Another difference between Session and SessionFactory is that former provides first level cache while SessionFactory provides the Second level cache.
Q31: How will you configure Hibernate?
A31: – To configure hibernate, you need hibernate.cfg.xml or hibernate.properties file and *.hbm.xml files, all these files are used by configuration class to create sessionFactory, which in turn creates the session instances.
– Session instances are the primary interface for persistence services.
– The hibernate.cfg.xml or hibernate.properties files are used to configure the hibernate service (database connection driver class, database connection URL, connection username, connection password, dialect, mapping resources etc.).
– The *hbm.xml files are used for mapping persistent objects to relational database.
– From Java 5 onwards you can configure and map persistent objects through annotations.
Q32: What are the Collection types in Hibernate?
Q33: What is the function performed by code generator in Hibernate?
A33: Code generator is a framework that allow simple and easy mapping of objects using pre-defined tags. It follows Model driven architecture which uses models of the objects separately and link them with each other. Code generator adds attributes to the source code. I uses the java-doc tags to generate the hibernate mapping. It is also used to generate the database definition language from the source code to map the objects correctly. It allows easy mapping of objects from an existing database and allow it to be connected to some different database model as well.
Q34: What is the role played by Session interface in Hibernate?
A34: Session interface is the primary interface used by Hibernate. It is the important interface for the applications that uses single thread, and a short lived object representing a conversation between application and the objects that are stored. This object allows creating query objects that can be used to call persistent objects from the database. This interface allows the wrapping of JDBC connection and keeps the transactions at one place. It holds the persistent objects in the cache for easy navigation and to increase the performance of the overall system. The interface is represented as :
Session session = sessionFactory.openSession();
Q35: Why Hibernate is preferred over JDBC?
A35: The preference of Hibernate is more due to the transparency it provides to the programmer for writing their code. It doesn’t allow the programmer to write explicit code to map the database tables to the application objects that are used during the interaction with RDBMS. It provides an advanced ORM solution to map the Java class files to the database tables for easy access and retrieval of the data. Hibernate uses Hibernate Query Language that is independent of the type of database used. It allows polymorphic queries to be retrieved and selects the way to perform the database manipulation tasks. Hibernate allow the storing of large amount of data in small files that can be easily accessed and the performance can be faster. It also provides the actually mapping to be performed between the tables and objects in the XML files.
Hibernate Conclusion Interview FAQs
We know the list of Hibernate Interview Questions and Answers, Hibernate Interview Questions and Answers Freshers, Hibernate Interview Questions and Answers, Hibernate Interview Questions is overwhelming but the advantages of reading all the questions will maximize your potential and help you crack the interview. The surprising fact is that this Hibernate interview questions and answers post covers all the basic of the Hibernate technology and you have to check out the FAQs of different components of Hibernate too.
However, you will be asked with the questions in the interview related to the above mentioned questions. Preparing and understanding all the concept of Hibernate technology will help you strengthen the other little information around the topic.
After preparing these interview questions, we recommend you to go for a mock interview before facing the real one. You can take the help of your friend or a Hibernate expert to find the loop holes in your skills and knowledge. Moreover, this will also allow you in practicing and improving the communication skill which plays a vital role in getting placed and grabbing high salaries.
Remember, in the interview, the company or the business or you can say the examiner often checks your basic knowledge of the subject. If your basics is covered and strengthened, you can have the job of your dream. The industry experts understand that if the foundation of the student is already made up, it is easy for the company to educate the employ towards advance skills. If there are no basics, there is no meaning of having learnt the subject.
Therefore, it’s never too late to edge all the basics of any technology. If you think that you’ve not acquired the enough skills, you can join our upcoming batch of Hibernate Training in Noida. We are one of the best institute for Hibernate in noida which provide advance learning in the field of Hibernate Course. We’ve highly qualified professionals working with us and promise top quality education to the students.
We hope that you enjoyed reading Hibernate Interview Questions and Answers, Hibernate Interview Questions and Answers Freshers, Hibernate Interview Questions and Answers, Hibernate Interview Questions and all the FAQs associated with the interview. Do not forget to revise all the Hibernate interview questions and answers before going for the Hibernate interview. In addition to this, if you’ve any doubt or query associated with Hibernate, you can contact us anytime. We will be happy to help you out at our earliest convenience. At last, we wish you all the best for your upcoming interview on Hibernate Technology.