• +91 9971497814
  • info@interviewmaterial.com

Hibernate Interview Questions Answers

Question 1 : How can I trim spaces from String data persisted to a CHAR column?

Answer 1 : Use a UserType.

Question 2 : How can I get access to O/R mapping information such as table and column names at runtime?

Answer 2 : This information is available via the Configuration object. For example, entity mappings may be obtained using Configuration.getClassMapping(). It is even possible to manipulate this metamodel at runtime and then build a new SessionFactory.

Question 3 : How can I insert XML data into Oracle using the xmltype() function?

Answer 3 : Specify custom SQL INSERT (and UPDATE) statements using <sql-insert> and <sql-update> in Hibernate3, or using a custom persister in Hibernate 2.1. You will also need to write a UserType to perform binding to/from the PreparedStatement.  

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 : How can I sort / order collection elements?

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

Answer 6 : 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 7 : How can I assign a default value to a property when the database column is null?

Answer 7 : Use a UserType.

Question 8 : Are collections pageable?

Answer 8 : Query q = s.createFilter( collection, "" ); // the trivial filterq.setMaxResults(PAGE_SIZE);q.setFirstResult(PAGE_SIZE * pageNumber);List page = q.list();

Question 9 : 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 9 : <generator class="foreign">     <param name="property">parent</param></generator>

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

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

Hibernate Contributors


Share your email for latest updates


Our partners