Hibernate Interview Questions

Hibernate Introduction
  1. What is Hibernate ?
  2. Briefly explain Hibernate Architecture ?
  3. What is the use of ORM ?
  4. What is Java Persistence API(JPA) ?
  5. What is JDBC ?
  6. Why are you using hibernate when we have JDBC ?
  7. What are the advantages of hibernate ?
  8. Tell me drawbacks or disadvantages of hibernate ?
  9. Mention design patterns used in hibernate ?
  10. What is the general flow of Hibernate communication with RDBMS?
  11. 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
    Create session factory from configuration object
    Get one session from this session factory
    Create HQL Query
    Execute query to get list containing Java objects
  12. How do you map Java Objects with Database tables?
  13. 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 :
    
      
       
       
     
    
  14. What are the most common methods of Hibernate configuration?
  15. The most common methods of Hibernate configuration are:
    Programmatic configuration
    XML configuration (hibernate.cfg.xml)
  16. What is HQL ?
  17. What is hibernate configuration file ?
  18. What is hibernate mapping file ?
  19. What is the root node in hbm.xml file ?
  20. Some important tags or elements used in hbm.xml ?
  21. Following are the important tags of hibernate.cfg.xml:
  22. Mention some important annotations used in hiberante mapping ?
  23. What are the core interfaces of hibernate ? or What are the key components/elements/objects of hibernate ?
  24. What are the key components of hibernate Configuration Object ?
  25. Name some properties required to configure database in standalone situation ?
  26. Some important methods and their usage ?
  27. Session.beginTransaction method begins a unit of work and returns the associated Transaction object. Session.createCriteria creates a new Criteria instance, for the given entity class, or a superclass of an entity class. Session.createQuery creates a new instance of Query for the given HQL query string. Session.createSQLQuery creates a new instance of SQLQuery for the given SQL query string. Session.delete removes a persistent instance from the datastore. Session.get returns the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance. Session.refresh re-reads the state of the given instance from the underlying database. Session.save saves the state of the given instance from the underlying database. Session.update updates the state of the given instance from the underlying database. Session.saveOrUpdate either saves(Object) or updates(Object) the given instance.
  28. What is meant by persistent class in hibernate ?
  29. What are the best practices hibernate recommend for persistant classes ?
  30. What is meant by serializable ?
  31. What are the states of object in hibernate ?
  32. Whether Session Factory and Session are thread safe objects ?
  33. Difference between openSession and getCurrentSession ?
  34. Difference between session.save() and session.persist() methods ?
  35. Difference between get() and load() methods in hibernate ?
  36. Difference between update() and merge() methods in hibernate ?
  37. Difference between save() , saveOrUpdate() , persist() methods ?
  38. Mention the types of association mapping available in hibernate ?
  39. How do you implement associations or relationships in hibernate ?
  40. Mention the inheritance models in hibernate ?
  41. What is meant by immutable class ? How can you make a class immutable in hibernate ?
  42. What is meant by automatic dirty check in hibernate ?
  43. Which association's are possible with Collections interface or with Collection classes ?
  44. What is hibernate Proxy ?
  45. What is meant by lazy loading in hibernate ?
  46. What is meant by hibernate caching ?
  47. What is meant by first level cache ?
  48. What is meant by second level cache ?
  49. What is meant by Query cache in hiberante ?
  50. Mention collection types in hibernate ?
  51. What is bag collection in hibernate ?
  52. How to fetch ordered collection from database in hibernate ?
  53. How can Hibernate be configured to access an instance variable directly and not through a setter method ?
  54. By mapping the property with access="field" in Hibernate metadata. This forces hibernate to bypass the setter method and access the instance variable directly while initializing a newly loaded object.

  55. Difference between ordered collection and sorted collection ? Which one is better in hibernate ?
  56. Tell me about joins in hibernate ?
  57. HQL provides four ways of expressing (inner and outer) joins:-
    An implicit association join
    An ordinary join in the FROM clause
    A fetch join in the FROM clause.
    A theta-style join in the WHERE clause.
    
  58. What is native sql query ? Can we execute them in hibernate ?
  59. What is the advantage of native sql query support in hibernate ?
  60. What is Named SQL Query ?
  61. What are the Mandatory things for a java object to become Hibernate Entity object ?
  62. What is Entity class ?
  63. What happens if we make our Entity class as final in hibenate ?
  64. Define cascade and inverse option in one-many mapping?
  65. 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?
  66. Explain about log4j logging in hibernate ?
  67. How to log SQL queries into log files in hibernate ?
  68. What is transaction Management ? How it works in hibernate ?
  69. A org.hibernate.Session is designed to represent a single unit of work (a single atmoic piece of work to be performed.
    Sample code of handling transaction from hibernate session.
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            Event theEvent = new Event();
            theEvent.setTitle(title);
            theEvent.setDate(theDate);
            session.save(theEvent);
            session.getTransaction().commit();
  70. What is Cascading ? Mention different types of Cascading ?
  71. How to configure application server JNDI DataSource with Hibernate Framework ?
  72. How to integrate Spring and Hibernate frameworks ?
  73. What is hibernate Validator Framework ?
  74. What are concurrency strategies ?
  75. What is N+1 Select problem in hibernate ? Mention some strategies to solve it ?
  76. How to implement Optimistic locking in Database?
  77. You can implement optimistic locks in your DB table in this way (This is how optimistic locking is done in Hibernate):
    - Add integer "version" column to your table.
    - Increase value of this column with each update of corresponding row.
    - To obtain lock, just read "version" value of row.
    - Add "version = obtained_version" condition to where clause of your update statement.
    - Verify number of affected rows after update. If no rows were affected - someone has already modified your entry.
    Your update should look like
       UPDATE mytable SET name = 'Andy', version = 3 WHERE id = 1 and version = 2;
    
  78. What are Callback interfaces? Callback interfaces allow the application to receive a notification when something interesting happens to an object—for example, when an object is loaded, saved, or deleted. Hibernate applications don't need to implement these callbacks, but they're useful for implementing certain kinds of generic functionality.
  79. What is transactional write-behind? Hibernate uses a sophisticated algorithm to determine an efficient ordering that avoids database foreign key constraint violations but is still sufficiently predictable to the user. This feature is called transactional write-behind.
  80. What do you mean by fetching strategy ? A fetching strategy is the strategy Hibernate will use for retrieving associated objects if the application needs to navigate the association. Fetch strategies may be declared in the O/R mapping metadata, or over-ridden by a particular HQL or Criteria query.
  81. What is the use of dynamic-insert and dynamic-update attributes in a class mapping?
  82. 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. dynamic-update (defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed dynamic-insert (defaults to false): Specifies that INSERT SQL should be generated at runtime and contain only the columns whose values are not null.
  83. How do you define sequence generated primary key in hibernate?
  84. Using  tag.
    Example:-
     
        
         SEQUENCE_NAME
       
    
  85. Define HibernateTemplate?
  86. 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.
  87. What are the benefits does HibernateTemplate provide?
  88. 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.
  89. How do you switch between relational databases without code changes?
  90. Using Hibernate SQL Dialects , we can switch databases. Hibernate will generate appropriate hql queries based on the dialect defined.
  91. If you want to see the Hibernate generated SQL statements on console, what should we do?
  92. In Hibernate configuration file set as follows: true
  93. What are derived properties?
  94. 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.
  95. What is component mapping in Hibernate?
  96. 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.
  97. What is Hibernate Criteria API ?
  98. 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();
  99. How do you invoke Stored Procedures?
  100. 
     
              
    
              
       
        { ? = call selectAllEmployees() }
     
    


2 comments:


  1. Very useful blog. Thanks for the information.

    visit: Hibernate Framework Training

    ReplyDelete
  2. Excellent blog admin, thanks for taking time to share this interview questions with answers. It is really helpful.
    Hibernate Training in Chennai | Hibernate Training

    ReplyDelete

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...