Java Hibernate Interview Questions & Answers part 1

0
901

Q: – What is hibernate (Java) ?

Hibernate is an object-relational mapping (ORM) library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database. Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions.

Q: – Why do you need ORM tool like Hibernate?

Hibernate is open source ORM tool. Hibernate reduces the time to perform database operations. Hibernate has advantages over using entity beans or JDBC calls. The implementation is done by just using POJOs. Creating a POJO may implement the Java bean mechanism of using setters and getters methods for assigning values and they can be used to persist on the database. By using a simple mapping file, the fields of class and the fields of the table is done. This mapping file is a XML file. So the flexibility in mapping is attained, instead of hard coding. IDEs like eclipse supports to create the description files.

Q: – What are the different types of Hibernate instance states ?

Three types of instance states:

  • Transient In this state, an instance is not associated with any persistence context
  • Persistent In this state, an instance is associated with a persistence context
  • Detached This is a state for an instance which was previously associated with a persistence context an has been currently closed dissociated

Q: – What are 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.

Q: – What is a SessionFactory?

SessionFactory is an interface and extends Referenceable, Serializable. The SessionFactory is the concept that is a single data store and thread safe. Because of this feature, many threads can access this concurrently and the sessions are requested, and also the cache that is immutable of compiled mappings for a specific database. A SessionFactory will be built only at the time of its startup. In order to access it in the application code, it should be wrapped in singleton. This wrapping makes the easy accessibility to it in an application code.

Q: – Explain the general flow of Hibernate communication with RDBMS ?

The general flow of Hibernate communication with RDBMS is :

  • The Hibernate configuration is to be loaded and creation of configuration object is done. The mapping of all hbm files will be performed automatically.
  • Creation of session factory from the configuration object.
  • Obtain a session from the session factory.
  • Creation of HQL Query
  • Execution of the query in order to get the list of containing java objects.

Q: – What is a HibernateTemplate?

HibernateTemplate is a helper class that is used to simplify the data access code. This class supports automatically converts HibernateExceptions which is a checked exception into DataAccessExceptions which is an unchecked exception. HibernateTemplate is typically used to implement data access or business logic services. The central method is execute(), that supports the Hibernate code that implements HibernateCallback interface.

Q: – What is 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.

Q: – 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.

 Submitted By:-Goyal Ankur            Email-ID: – goyal.ankur30@yahoo.in

SHARE

LEAVE A REPLY