JavaSpartans
Java J2ee Struts Hibernate Spring Interview Questions Java J2ee Struts Hibernate Spring Interview Questions
1 2 3 4 5    Next

1. What is Hibernate ?

Hibernate is a tool which hides the data accessing logic. Using Hibernate without writing a single SQL Query we can access the data from the database. Even if you have no knowledge of SQL, you can access data.
If the project size is very large, writing DAO's needs an expert level JDBC Programmer who can write minimal code with standards. But with out knowing all these we can use hibernate which will generate standard code to access data.
Hibernate is an ORM tool. Object Relational Mapping tool. We can store an employee object directly into the database using setters and getters.
Hibernate is a complete alternative for JDBC. If you want you can write SQL queries on your own but hibernate's main purpose is it will write SQL queries at background for you.
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 files. Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks.


2. Why Hibernate is more advantageous than EJB Entity Beans & JDBC?

-EJB Entity Beans are very fat. That?s why we will go for spring and Hibernate which are light weight compared to EJB.
-EJB Entity Beans work only inside of EJB Containers like JBoss, WebLogic, and WebSphere. But Hibernate can be used for Java Applications also. That means we can use hibernate outside of EJB Container also.
Hibernate is high performant compared to entity beans and hibernate uses two levels of cache which stores results in this cache, if same request comes, it will not hit the database, but fetches data from cache thus improves the performance.


3. What is the difference between hibernates save, update and saveOrUpdate methods ?

save - save method stores an object into the database. That means it insert an entry if the identifier doesn't exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update - update method in the hibernate is used for updating the object using identifier. If the identifier is missing or doesn't exist, it will throw exception.

saveOrUpdate - This method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called.

Try the following code.

package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateExample {
public static void main(String args[]){
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
EmployeeInfo employeeInfo = new EmployeeInfo();
employeeInfo.setSno(1);
employeeInfo.setName("HibernateTestSave");
session.save(employeeInfo);
transaction.commit();
session.close();

session = sessionFactory.openSession();
transaction = session.beginTransaction();
transaction.begin();
employeeInfo = new EmployeeInfo();
employeeInfo.setSno(1);
employeeInfo.setName("HibernateTestUpdate");
session.update(employeeInfo);
transaction.commit();
session.close();

session = sessionFactory.openSession();

transaction = session.beginTransaction();
transaction.begin();
employeeInfo = new EmployeeInfo();
employeeInfo.setSno(1);
employeeInfo.setName("HibernateTestSaveOrUpdate");
session.saveOrUpdate(employeeInfo);
transaction.commit();

session.close();
}
}



4. What do you mean by Named SQL query in hibernate ?

Named SQL queries are defined in the hibernate 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>
How to Invoke Named Query from Java code:
List people = session.getNamedQuery("empdetails").setString("TomBrady",name).setMaxResults(50).list();



5. What is the advantage of Hibernate over jdbc ?

Its a ORM framework
You can think in terms of oops concepts and persist object to a database table row.

It reduces repeated boiler plate code.

It simplifies complex relation ships among tables.

Hibernate provides a caching mechanism, which helps reduce the number of hits, as much as possible, that your application makes to the database server. This will have a considerable effect regarding the performance of your application. There is no such caching mechanism available in JDBC.

Database Independent

Hibernate's code is database independent because you do not need to change the HQL queries (with a few exceptions) when you change databases like MySQL, Oracle, etc. Hence, it is easy to migrate to a new database. It is achieved by using a friendly dialect to communicate with the database. The database can be specified using a dialect in the Hibernate configuration XML as follows.

<property name="dialect">org.hibernate.dialect.MySQL</property>

Below query helps to undertand this.

SQL TOP, LIMIT and ROWNUM Examples

SELECT TOP 3 * FROM Customers; (for SQL Server/MS Access):
SELECT * FROM Customers LIMIT 3; (for MySQL):
SELECT * FROM Customers WHERE ROWNUM <= 3; (for Oracle):

If you deal with query like above then you need to face troubles in switching databases.


6. How many levels of cache is maintained by hibernate. Explain ?

There are two levels of cache is maintained by hibernate.
Hibernate session object maintains a cache which is called as first level cache.
Hibernate session factory object maintains a second level cache.
four types of cache implementations are provided. They are EHCache (Easy Hibernate Cache), OSCache (Open Symphony Cache), SwarmCache,
JBossTreeCache.


7. What are steps of database persistence in hibernate?

Following are the steps that are followed while persisting an object in database using hibernate:
Step 1: Loads hibernate.cfg.xml.
Step 2: Creates Configuration object from hibernate.cfg.xml.
Step 3: Gets session from SessionFactory.
Step 4: Creates HQL.
Step 5: Executes HQL to get a list of java objects.


8. Why do you need ORM tools like hibernate?

The main advantage of ORM like hibernate is that it shields developers from messy SQL. 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


9. What is mean by Database Dialect in hibernate and What is the use of that ?

Hibernate's code is database independent because you do not need to change the HQL queries (with a few exceptions) when you change databases like MySQL, Oracle, etc. Hence, it is easy to migrate to a new database. It is achieved by using a friendly dialect to communicate with the database. The database can be specified using a dialect in the Hibernate configuration XML as follows.

<property name="dialect">org.hibernate.dialect.MySQL</property>

Below query helps to undertand this.

SQL TOP, LIMIT and ROWNUM Examples

SELECT TOP 3 * FROM Customers; (for SQL Server/MS Access):
SELECT * FROM Customers LIMIT 3; (for MySQL):
SELECT * FROM Customers WHERE ROWNUM <= 3; (for Oracle):

If you deal with query like above then you need to face troubles in switching databases.


10. How do you implement cacheing in hibernate application ?

To use the query cache, you must first activate it using the hibernate. cache. use_query_cache="true" property in the configuration file. By setting this property to true, you make Hibernate create the necessary caches in memory to hold the query and identifier sets.


Java J2ee Struts Hibernate Spring Interview Questions
1 2 3 4 5    Next
Menu - javaspartans.com - Java J2ee Struts Spring Hibernate Interview Questions
Hostens.com - A home for your website
Ads By Google