Gadgets

Wednesday, 27 January 2016

Hibernate Interview Questions and Answers

1. What is Hibernate?
Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source and lightweight ORM (Object Relational Mapping) tool.

2. What is ORM?
ORM stands for Object Relational Mapping. ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.


3. What are the advantages of Hibernate Framework?
=>Hibernate framework Open source and Lightweight
=>Fast performance
=>HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the     database independent queries.
=>Hibernate framework provides the facility to create the tables of the database automatically. 
=>Fetch data from multiple tables is easy in hibernate framework

4. What are the Databases supports by Hibernate?
Hibernate supports almost all the major databases. Here is some of the databases list.
=>HSQL Database Engine
=>MySQL
=>Oracle
=>DB2/NT
=>PostgreSQL
=>FrontBase
=>Microsoft SQL Server Database
=>Sybase SQL Server
5. Hibernate Architecture



//creating configuration object 
    Configuration cfg=new Configuration(); 
  
    //populates the data of the configuration file 
    cfg.configure("hibernate.cfg.xml");

    //creating seession factory object 
    SessionFactory factory=cfg.buildSessionFactory();
    
    //creating session object 
    Session session=factory.openSession();
       
    //creating transaction object
     Transaction t= session.beginTransaction();


6. What are the core interfaces of Hibernate?
The core interfaces of Hibernate framework are:
Configuration
SessionFactory
Session
Query
Criteria
Transaction

7. What is the Configuration?
Configuration object is the first Hibernate object, it create in any Hibernate application and usually created only once during application initialization. It represents a configuration or properties file required by the Hibernate.

8. What is the SessionFactory?
The SessionFactory is a factory of session. It holds second level cache of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session.
SessionFactory is a thread-safe object, many threads cannot access it simultaneously.

9. What is the Session?
The session object provides an interface between the application and data stored in the database.
It holds a first-level cache of data. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria.
Session is not a thread-safe object, many threads can access it simultaneously.

10. What is the Transaction?
The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction interface provides methods for transaction management.

11. What are the files are required to create Hibernate application?
  1. Create the Persistent class
  2. Create the mapping file for Persistent class
  3. Create the Configuration file
  4. Create the Application class that retrieves or stores the persistent object
  5. Load the required jar file

12. What is Persistent class?
Persistent class contains the getter and setter methods.

13. What is mapping file?
Mapping file instructs Hibernate how to map the defined class to the database tables. The mapping file name conventionally, should be class_name.hbm.xml.

14. What is the Configuration file?
Configuration file contains information about the database and mapping file. Conventionally, its name should be hibernate.cfg.xml.

15. What is the Application class?
In this class, we create SessionFactory, Session, Transaction objects to storing the data to the database. This class contain the main() method.

16. What is the Hibernate Query Language (HQL)?
Hibernate Query Language (HQL) is same as SQL (Structured Query Language) but it doesn't depends on the table of the database. Instead of table name, we use class name in HQL. So it is database independent query language.
Session.createQuery() method creates a new object of Query.

17. What is the Criteria Object?
Criteria object are used to create and execute object oriented criteria queries to retrieve objects. Session.createCriteria() method creates a new Criteria object.

18. What are the differences between save() and persist() method?
save()
persist()
session.save saves the object and returns the id of the object
persist() method do not return anything after saving the object.

19. What is the difference between get() and load() methods?
get()
load()
get() returns null if no data is present
load() method throws ObjectNotFoundException
It returns real object
It returns proxy object
get() method hit the database always
load() method doesn’t hit the database

20. What are the states of object in hibernate?
Transient: in this state if it is just created but has no primary key and not associated with session.
Persistent: in persistent state if session is open, and you just saved the instance in the database or retrieved the instance from the database.
Detached: object is in detached state if session is closed.

21. How many ways of association mapping are possible in hibernate?
4 types of association mapping in hibernate.
Many to One: A many-to-one association is the most common kind of association where an Object can be associated with multiple objects
Example: same college object can be associated with multiple student objects.
One to One: One to One mapping association where an Object can be associated with multiple objects, the only difference is the column unique constraint.
Example: college object can be associated with one single student object.
One to Many: One-to-Many mapping association, an object can be associated with multiple objects.
Many to Many: Many-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element.

22. What is the difference between first level and second level cache?
First level cache:
The first-level cache associated with the Session cache and it is a mandatory cache. It is enabled by default.
Second level cache:
Second Level Cache is associated with SessionFactory and it is optional. It is not enabled by default.

23. What are the inheritance mapping strategies?
3 ways of inheritance mapping in hibernate.
Table per hierarchy: In table per hierarchy mapping, single table is required to map the whole hierarchy
Table per concrete: table per concrete class, tables are created as per class. But duplicate column is added in subclass tables.
Table per subclass: tables are created as per class but related by foreign key. So there are no duplicate columns.


25. What is JDBC?

JDBC stands for Java Database Connectivity. It is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connect to the database.

No comments:

Post a Comment