-
What is ORM?Answer: ORM stands for Object Relational Mapping. It is a programming technique for converting data between object oriented programming language and relational database. In simple words it helps in persistence of java objects to tables in the data base.
-
What is Hibernate?Answer: Hibernate is a ORM framework that lets users to map java objects to relational tables using configuration files.
-
What is SessionFactory?Answer: SessionFactory is a factory to create hibernate Session objects. SessionFactory is threadsafe so that many threads can access it concurrently (use the same SessionFactory). A SessionFactory is usually built once at the start up. Below is the code to create a SessionFactory
//Creates a SessionFactory object SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
-
What is Session?Answer: Session represents a small unit of work in Hibernate. Session is a light weight object and it is not thread safe that is session objects cannot be shared by multiple threads. Session objects are created by SessionFactory and they are closed when all the work is completed.
//Creates a SessionFactory object SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //Creates a Session object Session session = sessionFactory.openSession();
-
What is first level cache?Answer: First level cache is maintained at the session object. By default hibernate uses first level cache. Caching is used by hibernate to reduce the number of SQL queries it needs to generate within a given transaction.
-
What is second level cache?Answer: Second level cache is associated at the SessionFactory object. The second level cache is available to the complete application. Hibernate first tries to retrieve the objects from first level cache that i.e. from session objects. If it cannot find then it goes to the second level cache which is SessionFactory. If hibernate cannot find objects in SessionFactory then it loads the objects from the database.
-
What is query cache?Answer: Query cache stores results of SQL query. Query cache can be used along with second level cache for improved performance. An example of query cache that hibernate supports is EhCache.
-
What is lazy initialization?Answer: Lazy initialization means, you do not initialize the entire object. You only initialize the first level variables member variables of the object and then you initialize the list only you need the list or only when we access the list. Click here to know more
-
What are the different object states in hibernate?Answer: There are three different object states in hibernate. They are:
- Transient
- Persistent
- Detached
-
Explain the difference between session.lock and session.update in hibernateAnswer: Both session.lock and session.update of session class are used to re-attach an object which has been detached earlier. But session.lock assumes that database is in sync with the detached objects and simply re-attaches the object to the session. Hence session.lock should be used very carefully.
-
What is criteria API?Answer: Criteria is a simplified API for retrieving by composing criterion objects. This is very convenient approach for functionality like "search" screens where there is a variable number for conditions to be placed upon the result search. To know more about criteria api "Click here"
Hibernate interview questions and answers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment