Hibernate Interview Questions And Answers
1.what is the
advantage of Hibernate over jdbc?
There are so many
1) Hibernate is data base independent, your code will work for all ORACLE,MySQL ,SQLServer etc.
In case of JDBC query must be data base specific. So hibernate based persistance logic is database independent persistance logic and JDBC based persistance logic is database dependent logic.
2) As Hibernate is set of Objects ,
3) No need to learn SQL language.You can treat TABLE as a Object . Only Java knowledge is need.
In case of JDBC you need to learn SQL.
3) Dont need Query tuning in case of Hibernate. If you use Criteria Quires in Hibernate then hibernate automatically tuned your query and return best result with performance.
In case of JDBC you need to tune your queries.
4) You will get benefit of Cache. Hibernate support two level of cache. First level and 2nd level. So you can store your data into Cache for better performance.
In case of JDBC you need to implement your java cache .
5) Hibernate supports Query cache and It will provide the statistics about your query and database status.
JDBC Not provides any statistics.
6) Development fast in case of Hibernate because you dont need to write queries
7) No need to create any connection pool in case of Hibernate. You can use c3p0.
In case of JDBC you need to write your own connection pool
8) In the xml file you can see all the relations between tables in case of Hibernate. Easy readability.
9) You can load your objects on start up using lazy=false in case of Hibernate.
JDBC Dont have such support.
10 ) Hibernate Supports automatic versioning of rows but JDBC Not.
1) Hibernate is data base independent, your code will work for all ORACLE,MySQL ,SQLServer etc.
In case of JDBC query must be data base specific. So hibernate based persistance logic is database independent persistance logic and JDBC based persistance logic is database dependent logic.
2) As Hibernate is set of Objects ,
3) No need to learn SQL language.You can treat TABLE as a Object . Only Java knowledge is need.
In case of JDBC you need to learn SQL.
3) Dont need Query tuning in case of Hibernate. If you use Criteria Quires in Hibernate then hibernate automatically tuned your query and return best result with performance.
In case of JDBC you need to tune your queries.
4) You will get benefit of Cache. Hibernate support two level of cache. First level and 2nd level. So you can store your data into Cache for better performance.
In case of JDBC you need to implement your java cache .
5) Hibernate supports Query cache and It will provide the statistics about your query and database status.
JDBC Not provides any statistics.
6) Development fast in case of Hibernate because you dont need to write queries
7) No need to create any connection pool in case of Hibernate. You can use c3p0.
In case of JDBC you need to write your own connection pool
8) In the xml file you can see all the relations between tables in case of Hibernate. Easy readability.
9) You can load your objects on start up using lazy=false in case of Hibernate.
JDBC Dont have such support.
10 ) Hibernate Supports automatic versioning of rows but JDBC Not.
2.What is Hibernate?
Hibernate is an open source, light weight Object
Relational Mapping tool to develop the database independent persistence login
in java and j2ee based applications.
Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using (XML) configuration and mapping files. Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks
Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using (XML) configuration and mapping files. Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks
3.What is ORM ?
ORM stands for object/relational mapping,
means providing the mapping between class with table and member
variables with columns is called ORM. ORM is the automated persistence of
objects in a Java application to the tables in a relational database.
4.hat does ORM consists of ?
An ORM solution consists of the following four
pieces:
· API for performing
basic CRUD operations
· API to express queries
referring to classes
· Facilities to specify
metadata
· Optimization
facilities : dirty checking,lazy associations fetching
5.What are the ORM
levels ?
The ORM levels are:
·
Pure relational
(stored procedure.)
·
Light objects mapping
(JDBC)
·
Medium object mapping
·
Full object Mapping
(composition,inheritance, polymorphism, persistence by reachability)
6.Why do you need ORM
tools like hibernate?
The main advantage of ORM like hibernate is that it
can develop the database independent persistence logic. Apart from
this, ORM provides following benefits:
· Improved
productivity
High-level object-oriented API
Less Java code to write
No SQL to write
· Improved
performance
Sophisticated caching
Lazy loading
Eager loading
· Improved
maintainability
A lot less code to write
· Improved
portability
ORM framework
generates database-specific SQL for you
7.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
8.What is the main difference
between Entity Beans and Hibernate ?
1)In Entity Bean at a time we can
interact with only one data Base. Where as in Hibernate we can able to
establishes the connections to more than One Data Base. Only thing we need to write one more configuration file.
2) EJB need container like Weblogic, WebSphare but hibernate don't nned. It can be run on tomcat.
3) Entity Beans does not support OOPS concepts where as Hibernate does.
4) Hibernate supports multi level cacheing, where as Entity Beans doesn't.
5) In Hibernate C3P0 can be used as a connection pool.
6) Hibernate is container independent. EJB not.
2) EJB need container like Weblogic, WebSphare but hibernate don't nned. It can be run on tomcat.
3) Entity Beans does not support OOPS concepts where as Hibernate does.
4) Hibernate supports multi level cacheing, where as Entity Beans doesn't.
5) In Hibernate C3P0 can be used as a connection pool.
6) Hibernate is container independent. EJB not.
9.What are the Core interfaces and
classes of Hibernate framework?
The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve
persistent objects and control
transactions.
·
Configuration class
(org.hibernate.cfg package)
·
Session interface (org.hibernate
package)
·
SessionFactory interface
(org.hibernate package)
·
Transaction interface (org.hibernate
package)
·
Query and Criteria interfaces
(org.hibernate package)
10.What is the general flow of
Hibernate communication with RDBMS?
The general flow of Hibernate communication with RDBMS is :
· Load the Hibernate configuration
file and create configuration object. It will automatically load all hbm
mapping files because mapping file can be configured in configuration file.
· Create session factory from
configuration object
· Get one session from this session
factory
· Create HQL Query
· Execute query to get list containing
Java objects.
11.What is the need for Hibernate
mapping file?
Hibernate mapping file is used to provides the mapping between java class with
table member variables with column names of the table. And also we can configure primary
key generation algorithm, relations and so on. Typical mapping file look as
follows:
12.What are the important tags of hibernate.cfg.xml?
This file can be used to provide the
database information like driverclass name, url, database usename, database
password, dialect, connection pooling mapping file and so on.
Following are the important tags of hibernate.cfg.xml:
Following are the important tags of hibernate.cfg.xml:
13.What role does the Session
interface play in Hibernate?
The main runtime interface between a Java application and Hibernate The Session
interface is the primary interface used by Hibernate applications. It is a
single-threaded, short-lived object representing a conversation between the
application and the persistent store. It allows you to create query objects to
retrieve persistent objects.
The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:
transient: never persistent, not associated with any Session
persistent: associated with a unique Session
detached: previously persistent, not associated with any Session
Session session = sessionFactory.openSession();
The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:
transient: never persistent, not associated with any Session
persistent: associated with a unique Session
detached: previously persistent, not associated with any Session
Session session = sessionFactory.openSession();
Session interface role:
· Wraps a JDBC connection
· Factory for Transaction
· Holds a mandatory (first-level)
cache of persistent objects, used when navigating the object graph or looking
up objects by identifier
14.What role does the SessionFactory
interface play in Hibernate?
SessionFactorys are immutable. The behaviour of a SessionFactory is controlled
by properties supplied at configuration time. These properties are defined on
Environment.
The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application—created during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
Implementors must be threadsafe.
SessionFactory sessionFactory = configuration.buildSessionFactory();
The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application—created during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
Implementors must be threadsafe.
SessionFactory sessionFactory = configuration.buildSessionFactory();
15.What are the most common ways to
specify the Hibernate configuration properties?
The most common methods of Hibernate configuration are:
· Programmatic configuration
By using setProperty(-) method of org.hibernate.cfg.Configuration.
· XML configuration
(hibernate.cfg.xml)
· By using .properties file
· By Using annotaions.(from Hibernate
3.3 on words)
16.How do you map Java Objects with
Database tables?
·
First we need to write Java domain
objects (beans with setter and getter).
· Write hbm.xml, where we map java
class to table and database columns to Java class variables.
Example :
<hibernate-mapping>
<class name="com.durgasoft.EmployeeBean" table="EMPLOYEE">
<id name=”eid” colume=”id”/>
<property name="ename" column="NAME" length="255"
not-null="true" type="java.lang.String"/>
<property name="address" column="ADDR" length="255"
not-null="true" type="java.lang.String"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.durgasoft.EmployeeBean" table="EMPLOYEE">
<id name=”eid” colume=”id”/>
<property name="ename" column="NAME" length="255"
not-null="true" type="java.lang.String"/>
<property name="address" column="ADDR" length="255"
not-null="true" type="java.lang.String"/>
</class>
</hibernate-mapping>
17.How do you define sequence
generated primary key algorithm in hibernate?
By using <id>, <generator> tags we can configure the primary key
and primary key generation algorithm.
Example:-
<id name="userid" column="USER_ID" type="java.lang.Long">
<generator class="sequence">
<param name="table">SEQ_NAME</param>
<generator>
</id>
Example:-
<id name="userid" column="USER_ID" type="java.lang.Long">
<generator class="sequence">
<param name="table">SEQ_NAME</param>
<generator>
</id>
18.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
19 . Difference between
getCurrentSession() and openSession() in Hibernate ?
getCurrentSession() :
Obtains the current session. The "current session" refers to a Hibernate Session bound by Hibernate behind the scenes, to the transaction scope.
A Session is opened when getCurrentSession() is called for the first time and closed when the transaction ends. It is also flushed automatically before the transaction commits. You can call getCurrentSession() as often and anywhere you want as long as the transaction runs. Only the Session that you obtained with sf.getCurrentSession() is flushed and closed automatically.
openSession() :
If you decide to use manage the Session yourself the go for sf.openSession() , you have to flush() and close() it.
It does not flush and close() automatically.
Example :
Transaction tx =session.berginTransaction();
Session session = factory.openSession();
try {
tx.begin();
// Do some work
session.createQuery(...);
session.persist(...);
session.flush(); // Extra work you need to do
tx.commit();
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}
finally {
session.close(); // Extra work you need to do
}
Obtains the current session. The "current session" refers to a Hibernate Session bound by Hibernate behind the scenes, to the transaction scope.
A Session is opened when getCurrentSession() is called for the first time and closed when the transaction ends. It is also flushed automatically before the transaction commits. You can call getCurrentSession() as often and anywhere you want as long as the transaction runs. Only the Session that you obtained with sf.getCurrentSession() is flushed and closed automatically.
openSession() :
If you decide to use manage the Session yourself the go for sf.openSession() , you have to flush() and close() it.
It does not flush and close() automatically.
Example :
Transaction tx =session.berginTransaction();
Session session = factory.openSession();
try {
tx.begin();
// Do some work
session.createQuery(...);
session.persist(...);
session.flush(); // Extra work you need to do
tx.commit();
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}
finally {
session.close(); // Extra work you need to do
}
20.What are the types of Hibernate
instance states ?
Three types of instance states:
Transient -The instance is not
associated with any persistence context
Persistent -The instance is
associated with a persistence context
Detached -The instance was
associated with a persistence context which has been closed – currently not
associated
0 comments:
Post a Comment