Hibernate Interview Questions

3 comments

Q: What is Hibernate?
A: Hibernate is a java-based Object/relational mapping(ORM) tool.

Q: What are ORM tools?
A: ORM tools provide automated solutions for the Object/relational paradigm mismatch problem, using metadata that describes the mapping between the objects and the database.

Q: What is Object/relational paradigm mismatch?
A: Object-Oriented paradigm is based on software engineering principles, whereas relational paradigm on mathematical principles. Object-oriented technology supports the building of applications out of networks of objects with both data and behavior. Relational technology supports the storage of data in tables and manipulation of that data using data manipulation language (DML). Because the underlying paradigms are different the two technologies do not work seamlessly, hence the name Object/relational paradigm mismatch.

Q: What role does the Session interface play in Hibernate?
A: The Session is a persistence manager that manages operation like storing and retrieving objects. Instances of Session are inexpensive to create and destroy. They are not threadsafe.

Q: What is SessionFactory interface?
A: The application obtains Session instances from a SessionFactory. SessionFactory instances are not lightweight and typically one instance is created for the whole application. If the application accesses multiple databases, it needs one per database

Q: What is Configuration interface?
A: The application uses a Configuration instance to specify the location of mapping documents and Hibernate-specific properties and then creates the SessionFactory.

Q: What is the naming convention for Hibernate XML mapping file extensions?
A: .hbm.xml

Q: What are the most common methods of configuring Hibernate?
A: 1. By placing hibernate.properties file in the classpath.
2. Including <property> elements in hibernate.cfg.xml in the classpath.

Q: How can the mapping files be configured in Hibernate?
A: 1. Mapping files can be added to Configuration in the application code or,
2. They can be configured in hibernate.cfg.xml using the <mapping> elements.

Q: What happens when both hibernate.properties and hibernate.cfg.xml are in the classpath?
A: The settings of the XML configuration file will override the settings used in the properties.

Q: Since SessionFactory instances are not lightweight, where is the single created instance placed in J2EE environments?
A: Usually it is bound to JNDI, it will bind itself automatically if hibernate.session_factory_name is set to the name of directory node.

Q: How to set Hibernate to log all generated SQL to the console?
A: By setting the hibernate.show_sql property to true.

Q: In hibernate, what interfaces/classes must the persistent classes (classes that are mapped to database tables) implement/extend?
A: NONE, they can be regular POJOs.

Q: Does hibernate require persistent classes to implement Serializable?
A: Hibernate doesn't require that persistent classes implement Serializable. However, when objects are stored in an HttpSession or passed by value using RMI, serialization is necessary.

Q: What methods must the persistent classes implement in Hibernate?
A: Since Hibernate instantiates persistent classes using Constructor.newInstance(), it requires a constructor with no arguments for every persistent class. And getter and setter methods for all the instance variables.

Q: How can Hibernate be configured to access a instance variable directly and not through a setter method?
A: By mapping the property with access="field" in Hibernate metadata. This forces hibernate to bypass the setter method and access the instance variable directly while initializing a newly loaded object.

Q: What is dirty checking in Hibernate?
A: Hibernate automatically detects object state changes in order to synchronize the updated state with the database, this is called dirty checking. An important note here is, Hibernate will compare objects by value, except for Collections, which are compared by identity. For this reason you should return exactly the same collection instance as Hibernate passed to the setter method to prevent unnecessary database updates.

Q: What is the root level element in a hibernate mapping file?
A: <hibernate-mapping>

Q: Is it possible to declare mappings for multiple classes in one mapping file?
A: Yes, by using multiple <class> elements. But, the recommended practice is to use one mapping file per persistent class.

Q: How are the individual properties mapped to different table columns?
A: By using multiple <property> elements inside the <class> element.

Q: What are derived properties?
A: The properties that are not mapped to a column, but calculated at runtime by evaluation of an expression are called derived properties. The expression can be defined using the formula attribute of the <property> element.

Q: How can you make a property be read from the database but not modified in anyway (make it immutable)?
A: By using the insert="false" and update="false" attributes.

Q: How can a whole class be mapped as immutable?
A: By using the mutable="false" attribute in the class mapping.

Q: What is the use of dynamic-insert and dynamic-update attributes in a class mapping?
A: They tell hibernate whether to include unmodified properties in SQL INSERT and SQL UPDATE.

Q: How do you achieve table-per-class hierarchy while mapping classes in Hibernate?
A: By using several <subclass> elements one for each sub-class inside the <class> element. The <subclass> class element can in turn contain other <subclass> elements.

Q: How do you achieve table-per-subclass while mapping classes in Hibernate?
A: By using <joined-subclass> element inside the <class> element. A <joined-subclass> element may contain other <joined-subclass> elements.

Q: Does hibernate allow mixing table-per-class hierarchy and table-per-subclass strategies?
A: No, you cannot have a <joined-subclass> inside <subclass> and vice versa.

3 comments: