• +91 9971497814
  • info@interviewmaterial.com

Hibernate Interview Questions Answers

Question 1 : I have a one-to-one association between two classes. Ensuring that associated objects have matching identifiers is bugprone. Is there a better way?

Answer 1 : <generator class="foreign">     <param name="property">parent</param></generator>

Question 2 : How do I use Hibernate in an EJB 2.1 session bean?

Answer 2 : 1. Look up the SessionFactory in JNDI. 2. Call getCurrentSession() to get a Session for the current transaction. 3. Do your work. 4. Don't commit or close anything, let the container manage the transaction.

Question 3 : What are the general considerations or best practices for defining your Hibernate persistent classes?

Answer 3 : 1.You must have a default no-argument constructor for your persistent classes and there should be getXXX() (i.e accessor/getter) and setXXX( i.e. mutator/setter) methods for all your persistable instance variables. 2.You should implement the equals() and hashCode() methods based on your business key and it is important not to use the id field in your equals() and hashCode() definition if the id field is a surrogate key (i.e. Hibernate managed identifier). This is because the Hibernate only generates and sets the field when saving the object. 3. It is recommended to implement the Serializable interface. This is potentially useful if you want to migrate around a multi-processor cluster. 4.The persistent class should not be final because if it is final then lazy loading cannot be used by creating proxy objects. 5.Use XDoclet tags for generating your *.hbm.xml files or Annotations (JDK 1.5 onwards), which are less verbose than *.hbm.xml files.

Question 4 : What is the difference between the session.update() method and the session.lock() method?

Answer 4 : Both of these methods and saveOrUpdate() method are intended for reattaching a detached object. The session.lock() method simply reattaches the object to the session without checking or updating the database on the assumption that the database in sync with the detached object. It is the best practice to use either session.update(..) or session.saveOrUpdate(). Use session.lock() only if you are absolutely sure that the detached object is in sync with your detached object or if it does not matter because you will be overwriting all the columns that would have changed later on within the same transaction. Note: When you reattach detached objects you need to make sure that the dependent objects are reatched as well.

Question 5 : What is the difference between the session.get() method and the session.load() method?

Answer 5 : Both the session.get(..) and session.load() methods create a persistent object by loading the required object from the database. But if there was not such object in the database then the method session.load(..) throws an exception whereas session.get(&) returns null.

Question 6 : Whats the easiest way to configure Hibernate in a plain Java application (without using JNDI)?

Answer 6 : Build a SessionFactory from a Configuration object. See the tutorials in the reference documentation.

Question 7 :  What are the benefits of detached objects?

Answer 7 : Detached objects can be passed across layers all the way up to the presentation layer without having to use any DTOs (Data Transfer Objects). You can later on re-attach the detached objects to another session.

Question 8 : How can I query for entities with empty collections?

Answer 8 : from Box boxwhere box.balls is empty Or, try this: select boxfrom Box box   left join box.balls ballwhere ball is null

Question 9 : Does Hibernate implement its functionality using a minimal number of database queries?

Answer 9 : Hibernate can make certain optimizations all the time: Caching objects. The session is a transaction-level cache of persistent objects. You may also enable a JVM-level/cluster cache to memory and/or local disk. Executing SQL statements later, when needed. The session never issues an INSERT or UPDATE until it is actually needed. So if an exception occurs and you need to abort the transaction, some statements will never actually be issued. Furthermore, this keeps lock times in the database as short as possible (from the late UPDATE to the transaction end). Never updating unmodified objects. It is very common in hand-coded JDBC to see the persistent state of an object updated, just in case it changed.....for example, the user pressed the save button but may not have edited any fields. Hibernate always knows if an object's state actually changed, as long as you are inside the same (possibly very long) unit of work. Efficient Collection Handling. Likewise, Hibernate only ever inserts/updates/deletes collection rows that actually changed. Rolling two updates into one. As a corollary to (1) and (3), Hibernate can roll two seemingly unrelated updates of the same object into one UPDATE statement. Updating only the modified columns. Hibernate knows exactly which columns need updating and, if you choose, will update only those columns. Outer join fetching. Hibernate implements a very efficient outer-join fetching algorithm! In addition, you can use subselect and batch pre-fetch optimizations. Lazy collection initialization. Lazy object initialization. Hibernate can use runtime-generated proxies (CGLIB) or interception injected through bytecode instrumentation at build-time. A few more (optional) features of Hibernate that your handcoded JDBC may or may not currently benefit from second-level caching of arbitrary query results, from HQL, Criteria, and even native SQL queries efficient PreparedStatement caching (Hibernate always uses PreparedStatement for calls to the database) JDBC 2 style batch updates Pluggable connection pooling Hopefully you will agree that Hibernate approaches the parsimony of the best hand-coded JDBC object persistence. As a subscript I would add that I have rarely seen JDBC code that approaches the efficiency of the "best possible" code. By co

Question 10 : What is hybernate in Java?

Answer 10 : Hibernate is an 'Object-Relational Mapping' library. Basically it allows you to define objects in Java that map to tables or views (or other structures) of a relational database. In Java you work with the objects defined, and they provide the service of interacting with the database - CRUD (create/read/update/delete).   Hibernate is an Object Relational mapping tool. One of the things it does is convert data from relational tables to objects. I think what you are implying is why just Java objects, why not objects in other languages. For .Net, I believe there is another framework: nhibernate. However, I have not used nHibernate   This is an example of how to handle a major 'impedance mismatch' between two separate worlds. The traditional RDBMS technology precedes Java, and is independent of it. It is oriented toward a storage approach that is not object oriented, although BLOBs and similary binary type content CAN be stored in most mainstream databases now. The Hibernate project does translation between the two worlds.

Hibernate Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners