• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Hibernate Interview Questions Answers

Question - 1 : - How can I sort / order collection elements?

Answer - 1 : - There are three different approaches: 1. Use a SortedSet or SortedMap, specifying a comparator class in the sort attribute or <set> or <map>. This solution does a sort in memory. 2. Specify an order-by attribute of <set>, <map> or <bag>, naming a list of table columns to sort by. This solution works only in JDK 1.4+. 3. Use a filter session.createFilter( collection, "order by ...." ).list()  

Question - 2 : - Can I map an inner class?

Answer - 2 : - You may persist any static inner class. You should specify the class name using the standard form ie. eg.Foo$Bar

Question - 3 : -  What are the pros and cons of detached objects?

Answer - 3 : - Pros: " When long transactions are required due to user think-time, it is the best practice to break the long transaction up into two or more transactions. You can use detached objects from the first transaction to carry data all the way up to the presentation layer. These detached objects get modified outside a transaction and later on re-attached to a new transaction via another session. Cons " In general, working with detached objects is quite cumbersome, and better to not clutter up the session with them if possible. It is better to discard them and re-fetch them on subsequent requests. This approach is not only more portable but also more efficient because - the objects hang around in Hibernate's cache anyway. " Also from pure rich domain driven design perspective it is recommended to use DTOs (DataTransferObjects) and DOs (DomainObjects) to maintain the separation between Service and UI tiers.

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

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

Question - 5 : - What is Middlegen?

Answer - 5 : - Middlegen is an open source code generation framework that provides a general-purpose database-driven engine using various tools such as JDBC, Velocity, Ant and XDoclet.

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

Answer - 6 : - 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 - 7 : - What is the difference between the session.update() method and the session.lock() method?

Answer - 7 : - 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 - 8 : - How can I trucate String data?

Answer - 8 : - Use a UserType.

Question - 9 : - How can I bind a dynamic list of values into an in query expression?

Answer - 9 : - Query q = s.createQuery("from foo in class Foo where foo.id in (:id_list)");q.setParameterList("id_list", fooIdList);List foos = q.list();

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

Answer - 10 : - 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.

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners