• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Hibernate Interview Questions Answers

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

Answer - 11 : - 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 - 12 : - How can I query for entities with empty collections?

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

Question - 13 : - How will you configure Hibernate?

Answer - 13 : - The configuration files hibernate.cfg.xml (or hibernate.properties) and mapping files *.hbm.xml are used by the Configuration class to create (i.e. configure and bootstrap hibernate) the SessionFactory, which in turn creates the Session instances. Session instances are the primary interface for the persistence service. " hibernate.cfg.xml (alternatively can use hibernate.properties): These two files are used to configure the hibernate sevice (connection driver class, connection URL, connection username, connection password, dialect etc). If both files are present in the classpath then hibernate.cfg.xml file overrides the settings found in the hibernate.properties file. " Mapping files (*.hbm.xml): These files are used to map persistent objects to a relational database. It is the best practice to store each object in an individual mapping file (i.e mapping file per class) because storing large number of persistent classes into one mapping file can be difficult to manage and maintain. The naming convention is to use the same name as the persistent (POJO) class name. For example Account.class will have a mapping file named Account.hbm.xml. Alternatively hibernate annotations can be used as part of your persistent class code instead of the *.hbm.xml files.

Question - 14 : - Are collections pageable?

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

Question - 15 : - How can I convert the type of a property to/from the database column type?

Answer - 15 : - Use a UserType.

Question - 16 : - How does Hibernate distinguish between transient (i.e. newly instantiated) and detached objects?

Answer - 16 : - Hibernate uses the version property, if there is one.  If not uses the identifier value. No identifier value means a new object. This does work only for Hibernate managed surrogate keys. Does not work for natural keys and assigned (i.e. not managed by Hibernate) surrogate keys.  Write your own strategy with Interceptor.isUnsaved().

Question - 17 : - What is hybernate in Java?

Answer - 17 : - 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 - 18 : - How does Hibernate perform?

Answer - 18 : - 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 - 19 : - Query Criteria: Not Equal

Answer - 19 : - 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 } }   ///////////////////////////////////////////////////////////////////////// <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping > <class name="Location" table="locations" lazy="true"> <id name="id" column="uid" type="long"> <generator class="native"/> </id> <property name="name" type="string"/> <property name="address" type="string"/> </class> </hibernate-mapping>   ///////////////////////////////////////////////////////////////////////// public class Location { private Long id; private String name; private String address; public Long ge

Question - 20 : - How can I assign a default value to a property when the database column is null?

Answer - 20 : - Use a UserType.

Computer Contributors

Rajeev Katiyar
Yes Baroda

Share your email for latest updates


Our partners