InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What are concurrency strategies? |
|
Answer» A concurrency strategy is a mediator which responsible for storing items of data in the cache and retrieving them from the cache. If you are going to enable a second-level cache, you will have to decide, for each persistent class and collection, which cache concurrency strategy to use.
|
|
| 2. |
What is second level cache in hibernate? |
|
Answer» Second level cache is an optional cache and first-level cache will always be consulted before any attempt is made to locate an object in the second-level cache. The second-level cache can be configured on a per-class and per-collection basis and mainly responsible for caching objects across sessions. |
|
| 3. |
What is first level cache in hibernate? |
|
Answer» The first-level cache is the Session cache and is a mandatory cache through which all requests must pass. The Session object keeps an object under its own power before committing it to the database. |
|
| 4. |
What is the difference between get() and load() methods of session object? |
|
Answer» There are following differences between get() and load() methods.
|
|
| 5. |
What is the difference between save() and persist() methods of session object? |
|
Answer» session.save saves the object and returns the id of the instance whereas persist do not return anything after saving the instance. |
|
| 6. |
Is Session a thread-safe object? |
|
Answer» No, Session is not thread-safe. |
|
| 7. |
Is SessionFactory a thread-safe object? |
|
Answer» Yes, SessionFactory is a thread-safe and can be accessed by multiple threads simultaneously. |
|
| 8. |
What is many-to-many association? |
|
Answer» A Many-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element. <many-to-many> element indicates that one object relates to many other objects and column attributes are used to link intermediate column. |
|
| 9. |
What is one-to-many association? |
|
Answer» In One-to-Many mapping association, an object can be can be associated with multiple objects. For example Employee object relates to many Certificate objects. A One-to-Many mapping can be implemented using a Set java collection that does not contain any duplicate element. <one-to-many> element of set element indicates that one object relates to many other objects. |
|
| 10. |
What is one-to-one association? |
|
Answer» A one-to-one association is similar to many-to-one association with a difference that the column will be set as unique. For example an address object can be associated with a single employee object. <many-to-one> element is used to define one-to-one association. The name attribute is set to the defined variable in the parent class. The column attribute is used to set the column name in the parent table which is set to unique so that only one object can be associated with an other object. |
|
| 11. |
What is many-to-one association? |
|
Answer» A many-to-one association is the most common kind of association where an Object can be associated with multiple objects. For example a same address object can be associated with multiple employee objects. <many-to-one> element is used to define many-to-one association. The name attribute is set to the defined variable in the parent class. The column attribute is used to set the column name in the parent table. |
|
| 12. |
Which element of hbm.xml is used to map a java.util.SortedMap property in hibernate? |
|
Answer» This is mapped with a <map> element and initialized with java.util.TreeMap. The sort attribute can be set to either a comparator or natural ordering. |
|
| 13. |
Which element of hbm.xml is used to map a java.util.Map property in hibernate? |
|
Answer» This is mapped with a <map> element and initialized with java.util.HashMap. |
|
| 14. |
Which element of hbm.xml is used to map a java.util.Collection property in hibernate? |
|
Answer» This is mapped with a <bag> or <ibag> element and initialized with java.util.ArrayList. |
|
| 15. |
Which element of hbm.xml is used to map a java.util.List property in hibernate? |
|
Answer» This is mapped with a <list> element and initialized with java.util.ArrayList. |
|
| 16. |
Which element of hbm.xml is used to map a java.util.SortedSet property in hibernate? |
|
Answer» This is mapped with a <set> element and initialized with java.util.TreeSet. The sort attribute can be set to either a comparator or natural ordering. |
|
| 17. |
Which element of hbm.xml is used to map a java.util.Set property in hibernate? |
|
Answer» This is mapped with a <set> element and initialized with java.util.HashSet. |
|
| 18. |
Which element of hbm.xml is used to map a Java class property to a column in the database table? |
|
Answer» The <property> element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. |
|
| 19. |
Which element of hbm.xml is used to automatically generate the primary key values? |
|
Answer» The <generator> element within the id element is used to automatically generate the primary key values. Set the class attribute of the generator element is set to native to let hibernate pick up either identity, sequence or hilo algorithm to create primary key depending upon the capabilities of the underlying database. |
|
| 20. |
Which element of hbm.xml defines maps the unique ID attribute in class to the primary key of the database table? |
|
Answer» The <id> element maps the unique ID attribute in class to the primary key of the database table. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. |
|
| 21. |
Which element of hbm.xml defines a specific mappings from a Java classes to the database tables? |
|
Answer» The <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute. |
|
| 22. |
What is root node of hbm.xml? |
|
Answer» The mapping document is an XML document having <hibernate-mapping> as the root element which contains all the <class> elements. |
|
| 23. |
Where Object/relational mappings are defined in hibernate? |
|
Answer» An Object/relational mappings are usually defined in an XML document. This mapping file instructs Hibernate how to map the defined class or classes to the database tables. We should save the mapping document in a file with the format <classname>.hbm.xml. |
|
| 24. |
What are the best practices that hibernate recommends for persistent classes. |
|
Answer» There are following main rules of persistent classes, however, none of these rules are hard requirements.
|
|
| 25. |
What are persistent classes in hibernate? |
|
Answer» Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate. |
|
| 26. |
Which method is used to save or update the state of the given instance from the underlying database? |
|
Answer» Session.saveOrUpdate either saves(Object) or updates(Object) the given instance. |
|
| 27. |
Which method is used to update the state of the given instance from the underlying database? |
|
Answer» Session.update updates the state of the given instance from the underlying database. |
|
| 28. |
Which method is used to save the state of the given instance from the underlying database? |
|
Answer» Session.save saves the state of the given instance from the underlying database. |
|
| 29. |
Which method is used to re-read the state of the given instance from the underlying database? |
|
Answer» Session.refresh re-reads the state of the given instance from the underlying database. |
|
| 30. |
Which method is used to get a persistent instance from the datastore? |
|
Answer» Session.get returns the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance. |
|
| 31. |
Which method is used to remove a persistent instance from the datastore? |
|
Answer» Session.delete removes a persistent instance from the datastore. |
|
| 32. |
Which method is used to create a SQL query? |
|
Answer» Session.createSQLQuery creates a new instance of SQLQuery for the given SQL query string. |
|
| 33. |
Which method is used to create a HQL query? |
|
Answer» Session.createQuery creates a new instance of Query for the given HQL query string. |
|
| 34. |
Which method is used to add a criteria to a query? |
|
Answer» Session.createCriteria creates a new Criteria instance, for the given entity class, or a superclass of an entity class. |
|
| 35. |
What is the purpose of Session.beginTransaction() method? |
|
Answer» Session.beginTransaction method begins a unit of work and returns the associated Transaction object. |
|
| 36. |
What are the three states of a persistent entity at a given point in time? |
|
Answer» Instances may exist in one of the following three states at a given point in time −
|
|
| 37. |
Name some of the properties you would require to configure for a databases in a standalone situation. |
||||||||||||||||
Answer»
|
|||||||||||||||||
| 38. |
What is Criteria in hibernate? |
|
Answer» Criteria object are used to create and execute object oriented criteria queries to retrieve objects. |
|
| 39. |
What is Query in hibernate? |
|
Answer» Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query. |
|
| 40. |
What is Transaction in hibernate? |
|
Answer» A Transaction represents a unit of work with the database and most of the RDBMS supports transaction functionality. Transactions in Hibernate are handled by an underlying transaction manager and transaction (from JDBC or JTA). This is an optional object and Hibernate applications may choose not to use this interface, instead managing transactions in their own application code. |
|
| 41. |
What is Session in hibernate? |
|
Answer» A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object. The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed. |
|
| 42. |
What is a SessionFactory in hibernate? |
|
Answer» Configuration object is used to create a SessionFactory object which inturn configures Hibernate for the application using the supplied configuration file and allows for a Session object to be instantiated. The SessionFactory is a thread safe object and used by all the threads of an application. The SessionFactory is heavyweight object so usually it is created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file. So if you are using multiple databases then you would have to create multiple SessionFactory objects. |
|
| 43. |
What is a configuration object in hibernate? |
|
Answer» The Configuration object is the first Hibernate object you create in any Hibernate application and usually created only once during application initialization. It represents a configuration or properties file required by the Hibernate. |
|
| 44. |
What are the two key components of a hibernate configuration object? |
|
Answer» The Configuration object provides two keys components −
This component creates the connection between the Java classes and database tables. |
|
| 45. |
What are the key components/objects of hibernate? |
|
Answer» Following are the key components/objects of Hibernate −
|
|
| 46. |
Name some of the java based tools/frameworks that supports hibernate integration. |
|
Answer» Hibernate supports a variety of other technologies, including the following −
|
|
| 47. |
Name some of the databases that hibernate supports. |
|
Answer» Hibernate supports almost all the major RDBMS. Following is list of few of the database engines supported by Hibernate.
|
|
| 48. |
What are the advantages of using Hibernate? |
|
Answer» Following are the advantages of using Hibernate.
|
|
| 49. |
Name some of the ORM frameworks based on JAVA. |
|
Answer» There are several persistent frameworks and ORM options in Java.
|
|
| 50. |
What are the advantages of ORM over JDBC? |
||||||||||||||||
|
Answer» An ORM system has following advantages over plain JDBC
|
|||||||||||||||||