Hibernate Java Interview Questions – Set 03

What is a SessionFactory? Is it a thread-safe object

SessionFactory is Hibernate’s concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in an application code.

SessionFactory sessionFactory = new Configuration( ).configure( ).buildSessionfactory( );

Explain about addjar() and addDirectory() methods

These methods are the most convenient to use in hibernate. These methods allow you to load all your Hibernate documents at a time. These methods simplify code configuration, refactoring, layout, etc. These functions help you to add your hibernate mapping to Hibernate initialization files.

Describe DML queriesofHibernate Query examples (HQL)

Hibernate created a new language named Hibernate Query Language (HQL), the syntax is quite similar to database SQL language. The main difference between is HQL uses class name instead of table name, and property names instead of column name.

HQL is extremely simple to learn and use, and the code is always self-explanatory.

  1. HQL Select Query Example

Retrieve a stock data where stock code is “7277″.

Query query = session.createQuery(“from Stock where stockCode = :code “);

query.setParameter(“code”, “7277”);

List list = query.list();

Query query = session.createQuery(“from Stock where stockCode = ‘7277’ “);

List list = query.list();

  1. HQL Update Query Example

Update a stock name to “DIALOG1″ where stock code is “7277″.

Query query = session.createQuery(“update Stock set stockName = :stockName” +

” where stockCode = :stockCode”);

query.setParameter(“stockName”, “DIALOG1”);

query.setParameter(“stockCode”, “7277”);

int result = query.executeUpdate();

Query query = session.createQuery(“update Stock set stockName = ‘DIALOG2′” +

” where stockCode = ‘7277’”);

int result = query.executeUpdate();

  1. HQL Delete Query Example

Delete a stock where stock code is “7277″.

Query query = session.createQuery(“delete Stock where stockCode = :stockCode”);

query.setParameter(“stockCode”, “7277”);

int result = query.executeUpdate();

Query query = session.createQuery(“delete Stock where stockCode = ‘7277’”);

int result = query.executeUpdate();

  1. HQL Insert Query Example

In HQL, only the INSERT INTO … SELECT … is supported; there is no INSERT INTO … VALUES. HQL only support insert from another table. For example

“insert into Object (id, name) select oo.id, oo.name from OtherObject oo”;

Insert a stock record from another backup_stock table. This can also called bulk-insert statement.

Query query = session.createQuery(“insert into Stock(stock_code, stock_name)” +

“select stock_code, stock_name from backup_stock”);

int result = query.executeUpdate();

The query.executeUpdate() will return how many number of record has been inserted, updated or deleted.

What are the most common methods of Hibernate configuration

The most common methods of Hibernate configuration are:

  • Programmatic configuration
  • XML configuration (hibernate.cfg.xml)

What are the important tags of hibernate.cfg.xml

An Action Class is an adapter between the contents of an incoming HTTP rest and the corresponding business logic that should be executed to process this rest.

In your experience, why would you use Spring framework

Spring has a layered architecture with over 20 modules to choose from. This means, use what you need and leave what you don’t need now. Spring simplifies JEE through POJO programming. There is no behind the scene magic in Spring as in JEE programming. POJO programming enables continuous integration and testability.

Spring framework’s core functionality is dependency injection (DI). Dependency injection promotes easy unit testing and more maintainable and flexible code. DI code is much easier to test. The functionality expressed by the object can be tested in a black box by building ‘mock’ objects implementing the interfaces expected by yourapplication logic. DI code is much easier to reuse as the ‘depended’ functionality is extrapolated into well defined interfaces, allowing separate objects whose configuration is handled by a suitable application platform to be plugged into other objects at will. DI code is more flexible. It is innately loosely coupled code to an extreme. This allows the programmer to pick and choose how objects are connected based exclusively on their required interfaces on one end and their expressed interfaces on the other.

Spring supports Aspect Oriented Programming (AOP), which enables cohesive development by separatingapplication business logic from system services. Supporting functionalities like auditing, gathering performance and memory metrics, etc can be enabled through AOP.

Spring also provides a lot of templates which act as base classes to make using the JEE standard technologies a breeze to work with. For example, the JdbcTemplate works well with JDBC, the JpaTemplate does good things with JPA, JmsTemplate makes JMS pretty straightforward. The RestTemplate is simply awesome in it’s simplicity. Simplicity means more readable and maintainable code.When writing software these days, it is important to try and decouple as much middleware code from your business logic as possible. The best approach when using remoting is to use Spring Remoting which can then use any messaging or remoting technology under the covers. Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration. Apache Camel is designed to work nicely with the Spring Framework in a number of ways.

It also provides declarative transactions, job scheduling, authentication, a fully-fledged MVC webframework, and integration to other frameworks likeHibernate,iBatis,JasperReports,JSF,Struts,Tapestry,Seam, Quartz job scheduler, etc.

Spring beans can be shared between different JVMs using Terracotta. This allows you to take existing beans and spread them across a cluster, turn Spring application context events into distributed events, export clustered beans via Spring JMX, and make your Spring applications highly available and clustered. Spring also integrate well with other clustering solutions like Oracle’s Coherance.Spring favors unchecked exceptions and eliminates unsightly try, catch, and finally (and some times try/catch within finally itself) blocks. The Spring templates like JpaTemplate takes care of closing or releasing a database connection. This prevents any potential resource leaks and promotes more readable code.It prevents the proliferation of factory and singleton pattern classes that need to be created to promote loose coupling if not for using a DI framework like Spring or Guice.

What are the Core interfaces are of Hibernate framework

The core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.

  • Session interface
  • SessionFactory interface
  • Transaction interface
  • Query and Criteria interfaces