Java Interview Questions Hibernate and Struts

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

1.

Hibernate:

1. What is hibernate and what are the advantage of hibernate ?


Hibernate is a high-performance Object/Relational persistence and query service which is licensed under the
open source GNU Lesser General Public License (LGPL) and is free to download. Hibernate not only takes
care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but
also provides data query and retrieval facilities.
Hibernate is an ORM implementation like many other implementations, it has a numerous number
of pros and cons. This section is mainly intended for listing down the most advantages of using Hibernate
beside for all these advantages listed for ORM.
1. Database Independent: Hibernate is independent of the database engine at the backend. List of
Hibernate Dialect are provided for connecting whatever database we prefer.
2. JPA Provider: Java Persistence API (JPA) is a specification. A lot of implementations are available for
JPA; Like EclipseLink, OpenJPA and much more. Hibernate is a standard ORM solution and it has a
JPA capability. Hence, using of hibernate would help you leverage the all capabilities of ORM and a
JPA in a JPA-specific projects.
3. Built-In Connection Pool Implementation: Hibernate has integrated automatically with the most
reliable connection pool implementation (C3P0).
4. Layered Architecture: Hibernate is a layered architecture, so that we don’t be obligated to use
everything provided by Hibernate. We just use those features that you may think they’re lightly
enough for the project.

2. What is caching , How many lavel of caching in hiberante , What is level 1 and level 2 caching in
hibernate ?
Caching is all about application performance optimization and it sits between your application and the
database to avoid the number of database hits as many as possible to give a better performance for
performance critical applications.
Caching is important to Hibernate as well which utilizes a multilevel caching schemes as explained below:
One of the primary concerns of mappings between a database and our Java application is performance.  This
is the common concern of the all guys who working with hibernate and spent the more time in ORM tools
for performance-enhancing changes to particular queries and retrievals. Today I want to discuss about some
facets of the Hibernate infrastructure that are implemented to handle certain performance concerns – 
The first-level cache - Session (Earlier hibernate already provide this level of cache)
The second-level cache -Session-factory-level cache
and the query cache.

The first-level cache: The first level cache type is the session cache. The session cache caches object within
the current session but this is not enough for long level i.e. session factory scope.
The second-level cache: The second-level cache is called 'second-level' because there is already a cache
operating for you in Hibernate for the duration you have a session open. A Hibernate Session is a
transaction-level cache of persistent data. It is possible to configure a SessionFactory-level cache on a class-
by-class and collection-by-collection basis.
second-level cache
Across sessions in an Application
Across applications (different applications on same servers with same database)
Across clusters (different applications on different servers with same database)

NOTE: Be careful Caches are never aware of changes made to the persistent store by another application i.e.
suppose one application deploy one server with using hibernate and get the data from database and put to
the cache for further using purpose but another application deployed another server which does not using
any ORM tool so it does mot know about Cache and direct interacting with database and may be update
data of database. Now data in Cache is invalid.

Hibernate uses first-level cache by default and you have nothing to do to use first-level cache. Let's go
straight to the optional second-level cache. Not all classes benefit from caching, so it's important to be able
to disable the second-level cache.

The 'second-level' cache exists as long as the session factory is alive. The second-level cache holds
on to the 'data' for all properties and associations (and collections if requested) for individual entities that
are marked to be cached. 

3. Comparision between JDBC and Hibernate


JDBC  Hibernate 

With JDBC, developer has to write code Hibernate is flexible and powerful ORM
to map an object model's data solution to map Java classes to database
representation to a relational data tables. Hibernate itself takes care of this
model and its corresponding database mapping using XML files so developer
schema.   does not need to write code for this. 

With JDBC, the automatic mapping of Hibernate provides transparent


Java objects with database tables and persistence and developer does not
vice versa conversion is to be taken care need to write code explicitly to map
of by the developer manually with lines database tables tuples to application
of code.   objects during interaction with RDBMS.  

JDBC supports only native Structured Hibernate provides a powerful query


Query Language (SQL). Developer has to language Hibernate Query Language
find out the efficient way to access (independent from type of database)
database, i.e. to select effective query that is expressed in a familiar SQL like
from a number of queries to perform syntax and includes full support for
same task.   polymorphic queries. Hibernate also
supports native SQL statements. It also
selects an effective way to perform a
database manipulation task for an
application.  

Application using JDBC to handle


persistent data (database tables) having
database specific code in large amount.
Hibernate provides this mapping itself.
The code written to map table data to
The actual mapping between tables and
application objects and vice versa is
application objects is done in XML files.
actually to map table fields to object
If there is change in Database or in any
properties. As table changed or
table then the only need to change XML
database changed then it’s essential to
file properties.  
change object structure as well as to
change code written to map table-to-
object/object-to-table. 

With JDBC, it is developer’s Hibernate reduces lines of code by


responsibility to handle JDBC result set maintaining object-table mapping itself
and convert it to Java objects through and returns result to application in form
code to use this persistent data in of Java objects. It relieves programmer
application. So with JDBC, mapping from manual handling of persistent
between Java objects and database data, hence reducing the development
tables is done manually.   time and maintenance cost.  

Hibernate, with Transparent


Persistence, cache is set to application
work space. Relational tuples are moved
to this cache as a result of query. It
With JDBC, caching is maintained by improves performance if client
hand-coding.   application reads same data many times
for same write. Automatic Transparent
Persistence allows the developer to
concentrate more on business logic
rather than this application code.  

In JDBC there is no check that always Hibernate enables developer to define


every user has updated data. This check version type field to application, due to
has to be added by the developer.   this defined field Hibernate updates
version field of database table every
time relational tuple is updated in form
of Java class object to that table. So if
two users retrieve same tuple and then
modify it and one user save this
modified tuple to database, version is
automatically updated for this tuple by
Hibernate. When other user tries to
save updated tuple to database then it
does not allow saving it because this
user does not have updated data.  

4. ORM
Object-relational mapping (ORM) is a mechanism that makes it possible to address, access and manipulate
objects without having to consider how those objects relate to their data sources

5. How many ways we can save object into DB using hibernate


Hibernate has a few different ways to save an object to the database, but the two main
ways are as follows:
session.save()
session.saveOrUpdate()
Or
save()
update()
saveOrUpdate()
saveOrUpdateCopy()
merge()
persist()
saveOrUpdate Calls either save or update depending on some checks. E.g. if no identifier exists, save is
called. Otherwise update is called.
save Persists an entity. Will assign an identifier if one doesn't exist. If one does, it's essentially doing an
update. Returns the generated ID of the entity.
update Attempts to persist the entity using an existing identifier. If no identifier exists, I believe an exception
is thrown.

saveOrUpdateCopy This is deprecated and should no longer be used. Instead there is...
merge Now this is where my knowledge starts to falter. The important thing here is the difference between
transient, detached and persistent entities. With save & update, you are dealing with persistent objects.
They are linked to a Session so Hibernate knows what has changed. But when you have a transient object,
there is no session involved. In these cases you need to use merge for updates and persist for saving.
persist As mentioned above, this is used on transient objects. It does not return the generated ID.

2. Struts
a. Struts flow , How request goes ?
Basic flow of struts 2 application by this simple figure:

1. User sends a request for the action


2. Controller invokes the ActionInvocation
3. ActionInvocation invokes each interceptors and action
4. A result is generated
5. The result is sent back to the ActionInvocation
6. A HttpServletResponse is generated
7. Response is sent to the user

You might also like