Latest Hibernate Interview Questions and Answers

Read Top Hibernate Interview Questions and Answers Experienced, Freshers for Job Interview Quiz with Solutions.

What are the benefits of HibernateTemplate?

The benefits of HibernateTemplate are:
• HibernateTemplate, which is a Spring Template class, can simplify the interactions with Hibernate Sessions.
• Various common functions are simplified into single method invocations.
• The sessions of hibernate are closed automatically
• The exceptions will be caught automatically, and converts them into runtime exceptions.

What is Hibernate proxy?

Mapping of classes can be made into a proxy instead of a table. A proxy is returned when actually a load is called on a session. The proxy contains actual method to load the data. The proxy is created by default by Hibernate, for mapping a class to a file. The code to invoke Jdbc is contained in this class.
Explain the types of Hibernate instance states.
The persistent class’s instance can be in any one of the three different states. These states are defined with a persistence context. The Hibernate has the following instance states:
• Transient: This instance is never been associated with any one of the persistence process. This does not have persistent identity like primary key value.
• Persistent: A persistent context is made to associate with the current instance. It has persistent identity like primary key value and a corresponding row of a table in the data base. Hibernate guarantees the persistent identity is equivalent to the java Identity [object], for a particular persistence context
• Detatched: This instance association with a persistence context is only once and the context was closed or serialized to another process. The persistent identity is retained and it can be a corresponding row in a database.

What are Collection types in Hibernate?

• ArrayType,
Constructor: ArrayType(String role, String propertyRef, Class elementClass, boolean isEmbeddedInXML)
• BagType,
Constructor: BagType(String role, String propertyRef, boolean isEmbeddedInXML)
• CustomCollectionType, A custom type for mapping user-written classes that implement PersistentCollection
Constructor: CustomCollectionType(Class userTypeClass, String role, String foreignKeyPropertyName, boolean isEmbeddedInXML)
• IdentifierBagType,
Constructor: IdentifierBagType(String role, String propertyRef, boolean isEmbeddedInXML)
• ListType,
Constructor: ListType(String role, String propertyRef, boolean isEmbeddedInXML)
• MapType,
Constructor: MapType(String role, String propertyRef, boolean isEmbeddedInXML)
• SetType
Constructor: SetType(String role, String propertyRef, boolean isEmbeddedInXML)

What is lazy initialization in hibernate?

The delaying the object creation or calculating a value or some process until the first time it is needed. The retrieval of particular information only at the time when the object is accessed, is lazy initialization in hibernate. A scenario for lazy initialization is:
When the field creation is expensive, a field may or may not be invoked.
In this scenario the creation of a field can be deferred until the actual moment is arise to use it. The performance is increased using this technique, by avoiding unnecessary creation of objects which is expensive and consumes the memory space.

What is lazy fetching in hibernate?

• Lazy setting decides whether to load child objects while loading the Parent Object.
• This can be done by a setting in hibernate mapping file of the parent class.Lazy = true
• By default the lazy loading of the child objects is true.

What is the difference between sorted and ordered collection in hibernate?

Sorted Collection:
The sorted collection is a collection that is sorted using the Java collections framework. The sorting is done in the memory of JVM that is running hibernate, soon after reading the data from the database using Java Comparator The less the collection the more the efficient of sorting
Ordered Collection:
The order collections will also sorts a collection by using the order by clause for the results fetched.
The more the collection, the more efficient of sorting.

Explain the advantages and disadvantages of detached objects

• Detached objects passing can be done across layers upto the presentation layer without using Data Transfer Objects.
• At the time of using long transactions by the user which needs long think-time, it is suggested to split these transactions into some transactions. The detached objects get modified apart from the transaction scope which then can be re-attached to a new transaction.
• The usage of detached objects are cumbersome and cryptic. It is suggested not to be cluttered with the session, if possible.
• It is recommended to use DataTransferObjects and DomainObjects that is used to maintain separation between the user interfaces and the Service.

What is Hibernate Query Language (HQL)?

Hibernate Query Language is designed for data management using Hibernate technology. It is completely object oriented and hence has notions like inheritance, polymorphism and abstraction. The queries are case-sensitive. This has an exception for Java classes and properties. The query operations are through objects. HQL acts as a bridge between Objects and RDBMS.

Explain the role of Session interface in Hibernate

• In hibernate, the Session interface wraps a JDBC connection, holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier and is a factory for Transaction
• Session session = sessionFactory.openSession();
• The Session interface is the primary interface used by Hibernate applications.
• It is a single-threaded, short-lived object representing a conversation between the application and the persistent store.
• It allows you to create query objects to retrieve persistent objects.

State the role of SessionFactory interface plays in Hibernate.

• An application obtains Session instances from a SessionFactory which is typically single for the whole application created during its initialization.
• The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime.
• It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
SessionFactory sessionFactory = configuration.buildSessionFactory();

What is the difference between merge and update?

update () : When the session does not contain an persistent instance with the same identifier, and if it is sure use update for the data persistence in hibernate.
merge (): Irrespective of the state of a session, if there is a need to save the modifications at any given time, use merge().

What is the advantage of Hibernate over jdbc?

The advantages of Hibernate over JDBC are:
• Hibernate code will work well for all databases, for ex: Oracle,MySQL, etc. where as JDBC is database specific.
• No knowledge of SQL is needed because Hibernate is a set of objects and a table is treated as an object, where as to work with JDBC, one need to know SQL.
• Query tuning is not required in Hibernate. The query tuning is automatic in hibernate by using criteria queries, and the result of performance is at its best. Where as in JDBC the query tuning is to be done by the database authors.
• With the support of cache of hibernate, the data can be placed in the cache for better performance. Where as in JDBC the java cache is to be implemented.

Why hibernate is advantageous over Entity Beans & JDBC?

An entity bean always works under the EJB container, which allows reusing of the object external to the container. An object can not be detached in entity beans and in hibernate detached objects are supported.
Hibernate is not database dependent where as JDBC is database dependent. Query tuning is not needed for hibernate as JDBC is needed. Data can be placed in multiple cache which is supported by hibernate, whereas in JDBC the cache is to be implemented.

Explain the main difference between Entity Beans and Hibernate

Entity beans are to be implemented by containers, classes, descriptors. Hibernate is just a tool that quickly persist the object tree to a class hierarchy in a database and without using a single SQL statement. The inheritance and polymorphism is quite simply implemented in hibernate which is out of the box of EJB and a big drawback.

Explain the difference between Hibernate and Spring.

Hibernate is an ORM tool for data persistency. Spring is a framework for enterprise applications. Spring supports hibernate and provides the different classes which are templates that contains the common code.


Popular posts from this blog

TOP Agile Testing Interview Questions and Answers

Latest Agile Testing Interview Questions and Answers

Most Asked ADO.NET Interview Questions and Answers