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.

Why do you need an ORM tool like Hibernate?

Hibernate is an 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 the 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.

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 that was previously associated with a persistence context and has been currently closed dissociated

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 needs 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. Whereas 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. Whereas in JDBC the java cache is to be implemented.

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.

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.

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(), which supports the Hibernate code that implements HibernateCallback interface.

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 contain the common code.

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.

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 the 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.

What is the difference between merge and update?

update (): When the session does not contain a 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 difference between load() and get() in Hibernate?

Difference between load() and get() load() Use this method if it is sure that the objects exist. The load() method throws an exception,when the unique id could not found in the database. The load() method returns proxy by default and the data base will not be effected until the invocation of the proxy. get() Use this method if it is not sure that the objects exist. Returns null when the unique id is unavailable in the database. The database will be affected immediately.

what is 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 persists the object tree to a class hierarchy in a database and without using a single SQL statement. The inheritance and polymorphism are quite simply implemented in hibernate which is out of the box of EJB and a big drawback.

What is ORM?

Object Relational Model is a programming technique. This technique is used to convert the data from an incompatible type to that of a relational database. The mapping is done by using the OOP languages. For example, every table in a table is represented by an object.

The purpose of ORM is an application is allowed and written OOP language that is to deal with the data that is manipulated in the forms objects. The class-level attributes are ‘mapped’ to tables.

What is lazy fetching in hibernate?

Lazy fetching is associated with child objects loading for its parents. While loading the parent, the selection of loading a child object is to be specified/mentioned in the hbm.xml file. Hibernate does not load the whole child objects by default. Lazy=true means not to load the child objects.

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 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.

What are the core interfaces of Hibernate framework?

1. Session Interface: The basic interface for all hibernate applications. The instances are light weighted and can be created and destroyed without an expensive process.
2. SessionFactory interface: The delivery of session objects to hibernate applications is done by this interface. For the whole application, there will be generally one SessionFactory and can be shared by all the application threads.
3. Configuration Interface: Hibernate bootstrap action is configured by this interface. The location specification is specified by specific mapping documents, is done by the instance of this interface.
4. Transaction Interface: This is an optional interface. This interface is used to abstract the code from a transaction that is implemented such as a JDBC / JTA transaction.
5. Query and Criteria interface: The queries from the user are allowed by this interface apart from controlling the flow of the query execution.

Explain the role of Session interface in Hibernate.

A session interface is a single-threaded object. The representation of a single unit of work with the Java application and the persistence database is done by this object. The wrapping of a JDBC connection is the emphasized role of a Session interface object. This is the major interface between Java application and Hibernate. This is a runtime interface. The beginning and ending of a transaction cycle is bounded by the Session interface. The purpose of this interface is to perform create, read and delete operations for the objects that are mapped with entity classes.

Explain with code sample how to create a custom control?

Steps to create a custom control:

1. Create a new project.
2. Add a custom control to the project.
3. Change the name of the class you need to inherit the control from the base class. E.g. inherit the class from System.Windows.Forms.Button if the control s to be inherited from a button class.
4. Implement the control with custom properties and featured needed by the control.
5. Override the OnPaint method if the control’s appearance needs to be changed.
6. Save the build the control
7. Reference you control into another or the same project and use the control.