• +91 9723535972
  • info@interviewmaterial.com

Hibernate Interview Questions and Answers

Hibernate Interview Questions and Answers

Question - 11 : - What is hybernate in Java?

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

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

Answer - 12 : - 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 - 13 : - Hibernate uses so much runtime reflection?

Answer - 13 : - Many former C or C++ programmers prefer generated-code solutions to runtime reflection. This is usually justified by reference to the performance red-herring. However, modern JVMs implement reflection extremely efficiently and the overhead is minimal compared to the cost of disk access or IPC. Developers from other traditions (eg. Smalltalk) have always relied upon reflection to do things that C/C++ needs code-generation for. In the very latest versions of Hibernate, "reflection" is optimised via the CGLIB runtime bytecode generation library. This means that "reflected" property get / set calls no longer carry the overhead of the Java reflection API and are actually just normal method calls. This results in a (very) small performance gain.

Question - 14 : - How does Hibernate perform?

Answer - 14 : - We claim that Hibernate performs well, in the sense that its performance is limited by the underlying JDBC driver / relational database combination. Given this, the question boils down to: does Hibernate implement its functionality using a minimal number of database queries and how can it improve performance and scalability on top of JDBC? This page hopefully answers these questions.

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

Answer - 15 : - 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 - 16 : - Whats the easiest way to configure Hibernate in a plain Java application (without using JNDI)?

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

Question - 17 : - Query Criteria Equal

Answer - 17 : - import java.util.*; import org.hibernate.*; import org.hibernate.criterion.*; public class SimpleRetrieveTest { public static void main(String[] args) { HibernateUtil.setup("create table EVENTS ( uid int, name VARCHAR, start_Date date, duration int, location_id int);"); HibernateUtil.setup("create table locations ( uid int, name VARCHAR, address VARCHAR);"); // hibernate code start Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Location location = new Location(); location.setName("USA"); location.setAddress("Address"); session.save(location); Event e= new Event(); e.setId(11111L); e.setName("testSave"); e.setStartDate(new Date()); e.setLocation(location); session.save(e); Criteria crit = session.createCriteria(Event.class); crit.add( Expression.eq( "name", "testSave" ) ); crit.setMaxResults(1); List results = crit.list(); System.out.println(results.size()); tx.commit(); HibernateUtil.closeSession(); HibernateUtil.sessionFactory.close(); // hibernate code end } }   /////////////////////////////////////////////////////////////////////////     ///////////////////////////////////////////////////////////////////////// public class Location { private Long id; private String name; private String address; publi

Question - 18 : - Query Criteria: Not Equal

Answer - 18 : - import java.util.*; import org.hibernate.*; import org.hibernate.criterion.*; public class SimpleRetrieveTest { public static void main(String[] args) { HibernateUtil.setup("create table EVENTS ( uid int, name VARCHAR, start_Date date, duration int, location_id int);"); HibernateUtil.setup("create table locations ( uid int, name VARCHAR, address VARCHAR);"); // hibernate code start Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Location location = new Location(); location.setName("USA"); location.setAddress("Address"); session.save(location); Event e= new Event(); e.setId(11111L); e.setName("testSave"); e.setStartDate(new Date()); e.setLocation(location); session.save(e); Criteria crit = session.createCriteria(Event.class); crit.add( Expression.ne( "name", "noName" ) ); crit.setMaxResults(1); List results = crit.list(); System.out.println(results.size()); tx.commit(); HibernateUtil.closeSession(); HibernateUtil.sessionFactory.close(); // hibernate code end } }   /////////////////////////////////////////////////////////////////////////   ///////////////////////////////////////////////////////////////////////// public class Location { private Long id; private String name; private String address; public Long ge

Question - 19 : - Middlegen 2.1 Released!

Answer - 19 : - Overview of changes/fixes: Hibernate plugin - various fixes and patches. Torque plugin (new) - generate Torque config files. XMI plugin (new) - generate some XMI diagrams from the generated code. Adapter plugin (new) - making it easier to switch persistence layer for generated Struts/JSP code. Many core/plugin bugs fixed.

Question - 20 : - What is Middlegen?

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


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners