Monday, 2 February 2015

Basic Hibernate Interview Questions and Answers (Part4)

31. Why do you need ORM tool like Hibernate?
ORM tools like hibernate provide following benefits:
Improved performance: Lazy loading, Sophisticated caching, Eager loading
Improved productivity: High-level object-oriented API, Less Java code to write, No SQL to write
Improved maintainability: A lot less code to write
Improved portability: ORM framework generates database-specific SQL for you

32. What are the main advantages of ORM like hibernate?
Hibernate implements extremely high-concurrency architecture with no resource-contention issues. This architecture scales extremely well as concurrency increases in a cluster or on a single machine.
Other performance related optimizations that hibernate performs are:
Caching objects
Executing SQL statements later, when needed
Never updating unmodified objects
Efficient Collection Handling
Rolling two updates into one
Updating only the modified columns
Outer join fetching
Lazy collection initialization
Lazy object initialization

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

34. Explain how to configure Hibernate.
Answer : 1
Hibernate uses a file by name hibernate.cfg.xml. This file creates the connection pool and establishes the required environment. A file named .hbm.xml is used to author mappings. The bootstrap action is configured by using Configuration interface.
There are two types of environment to configure hibernate:
1. Managed Environment: The definitions of database operations such as connections, transaction boundaries, security levels. This environment is provided by application servers such as Jboss,Weblogic,Websphere.
2. Non-managed Environment: The basic configuration template is provided by this interface. Tomcat is one of the examples that best supports this environment.

Answer :2
Programmatic configuration
The org.hibernate.cfg.Configuration instance can be instantiated directly by specifying XML mapping documents. If the mapping files are in the classpath, use addResource().
Configuration cfg = new Configuration()
An alternative way is to specify the mapped class and allow Hibernate to find the mapping document:
Configuration cfg = new Configuration()
org.hibernate.cfg.Configuration also allows you to specify configuration properties:
Configuration cfg = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
.setProperty("hibernate.order_updates", "true");
Alternative options include:
Passing an instance of java.util.Properties to Configuration.setProperties().
Placing a file named in a root directory of the classpath.
Setting System properties using java -Dproperty=value.
Including <property> elements in hibernate.cfg.xml (this is discussed later).
Hibernate JDBC Properties:

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

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

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

38. What are Collection types in Hibernate?
Constructor: ArrayType(String role, String propertyRef, Class elementClass, boolean isEmbeddedInXML)
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)
Constructor: IdentifierBagType(String role, String propertyRef, boolean isEmbeddedInXML)
Constructor: ListType(String role, String propertyRef, boolean isEmbeddedInXML)
Constructor: MapType(String role, String propertyRef, boolean isEmbeddedInXML)
Constructor: SetType(String role, String propertyRef, boolean isEmbeddedInXML)

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

40. 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.
More Questions & Answers :-

No comments:

Post a Comment