Hibernate

  Home  Java Programing  Hibernate


“Hibernate Interview Questions and Answers will guide us now that 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. Learn more about Hibernate bu this Hibernate Interview Questions with Answers guide”



52 Hibernate Questions And Answers

21⟩ What should SessionFactory be placed so that it can be easily accessed?

As far as it is compared to J2EE environment, if the SessionFactory is placed in JNDI then it can be easily accessed and shared between different threads and various components that are hibernate aware. You can set the SessionFactory to a JNDI by configuring a property hibernate.session_factory_name in the hibernate.properties file.

 110 views

22⟩ What Does Hibernate Simplify?

Hibernate simplifies:

* Saving and retrieving your domain objects

* Making database column and table name changes

* Centralizing pre save and post retrieve logic

* Complex joins for retrieving related items

* Schema creation from object model

 131 views

23⟩ What are Callback interfaces?

These interfaces are used in the application to receive a notification when some object events occur. Like when an object is loaded, saved or deleted. There is no need to implement callbacks in hibernate applications, but they're useful for implementing certain kinds of generic functionality.

 176 views

24⟩ What is the difference between and merge and update?

Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.

 146 views

25⟩ What the Core interfaces are of hibernate framework?

There are many benefits from these. Out of which the following are the most important one.

Session Interface : This is the primary interface used by hibernate applications. The instances of this interface are lightweight and are inexpensive to create and destroy. Hibernate sessions are not thread safe.

SessionFactory Interface : This is a factory that delivers the session objects to hibernate application. Generally there will be a single SessionFactory for the whole application and it will be shared among all the application threads.

Configuration Interface : This interface is used to configure and bootstrap hibernate. The instance of this interface is used by the application in order to specify the location of hibernate specific mapping documents.

Transaction Interface : This is an optional interface but the above three interfaces are mandatory in each and every application. This interface abstracts the code from any kind of transaction implementations such as JDBC transaction, JTA transaction.

Query and Criteria Interface : This interface allows the user to perform queries and also control the flow of the query execution.

 121 views

27⟩ What is the difference between load() and get()?

load() vs. get()

load() :-

Only use the load() method if you are sure that the object exists.

load() method will throw an exception if the unique id is not found in the database. load() just returns a proxy by default and database won’t be hit until the proxy is first invoked.

get():-

If you are not sure that the object exists, then use one of the get() methods.

get() method will return null if the unique id is not found in the database.

get() will hit the database immediately.

 139 views

28⟩ How do you invoke Stored Procedures?

<sql-query name=”selectAllEmployees_SP” callable=”true”>

<return alias=”emp” class=”employee”>

<return-property name=”empid” column=”EMP_ID”/>

<return-property name=”name” column=”EMP_NAME”/>

<return-property name=”address” column=”EMP_ADDRESS”/>

{ ? = call selectAllEmployees() }

</return>

</sql-query>

 144 views

29⟩ What do you mean by Named - SQL query?

Named SQL queries are defined in the mapping xml document and called wherever required.

Example:

<sql-query name = “empdetails”>

<return alias=”emp” class=”com.test.Employee”/>

SELECT emp.EMP_ID AS {emp.empid},

emp.EMP_ADDRESS AS {emp.address},

emp.EMP_NAME AS {emp.name}

FROM Employee EMP WHERE emp.NAME LIKE :name

</sql-query>

Invoke Named Query :

List people = session.getNamedQuery(”empdetails”)

.setString(”TomBrady”, name)

.setMaxResults(50)

.list();

 135 views

30⟩ Define cascade and inverse option in one-many mapping?

cascade – enable operations to cascade to child entities.

cascade=”all|none|save-update|delete|all-delete-orphan”

inverse – mark this collection as the “inverse” end of a bidirectional association.

inverse=”true|false”

Essentially “inverse” indicates which end of a relationship should be ignored, so when persisting a parent who has a collection of children, should you ask the parent for its list of children, or ask the children who the parents are?

 130 views

31⟩ What are POJOs?

POJO stands for plain old java objects. These are just basic JavaBeans that have defined setter and getter methods for all the properties that are there in that bean. Besides they can also have some business logic related to that property. Hibernate applications works efficiently with POJOs rather then simple java classes.

 125 views

32⟩ Explain Criteria API?

Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like “search” screens where there is a variable number of conditions to be placed upon the result set.

Example :

List employees = session.createCriteria(Employee.class)

.add(Restrictions.like(”name”, “a%”) )

.add(Restrictions.like(”address”, “Boston”))

.addOrder(Order.asc(”name”) )

.list();

 122 views

33⟩ What does it mean to be inverse?

It informs hibernate to ignore that end of the relationship. If the one–to–many was marked as inverse, hibernate would create a child–>parent relationship (child.getParent). If the one–to–many was marked as non–inverse then a child–>parent relationship would be created.

 130 views

34⟩ What are the benefits does HibernateTemplate provide?

The benefits of HibernateTemplate are :

* HibernateTemplate, a Spring Template class simplifies interactions with Hibernate Session.

* Common functions are simplified to single method calls.

* Sessions are automatically closed.

* Exceptions are automatically caught and converted to runtime exceptions.

 113 views

36⟩ Define HibernateTemplate?

org.springframework.orm.hibernate.HibernateTemplate is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.

 136 views

37⟩ What are derived properties?

The properties that are not mapped to a column, but calculated at runtime by evaluation of an expression are called derived properties. The expression can be defined using the formula attribute of the element.

People who read this also read:

Core Java Questions

Spring Questions

SCJP 6.0 Certification

EJB Interview Questions

Servlets Questions

 120 views

38⟩ What is component mapping in Hibernate?

* A component is an object saved as a value, not as a reference

* A component can be saved directly without needing to declare interfaces or identifier properties

* Required to define an empty constructor

* Shared references not supported

 124 views

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

sorted collection vs. order collection

sorted collection :-

A sorted collection is sorting a collection by utilizing the sorting features provided by the Java collections framework. The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator.

If your collection is not large, it will be more efficient way to sort it.

order collection :-

Order collection is sorting a collection by specifying the order-by clause for sorting this collection when retrieval.

If your collection is very large, it will be more efficient way to sort it .

 127 views