Hibernate Questions and Answers
1. What’s Hibernate?
|Hibernate is a popular framework of Java which allows an efficient Object Relational mapping using configuration files in XML format. After java objects mapping to database tables, database is used and handled using Java objects without writing complex database queries.|
2. What are the different levels of ORM quality?
|Four levels defined for ORM quality: Pure relational Light object mapping Medium object mapping Full object mapping|
3. What is HibernateTemplate?
|The spring framework provides HibernateTemplate (org.springframework.orm.hibernate.HibernateTemplate) which is kind of helper class and provides following benefits. - HibernateTemplate class simplifies interaction with Hibernate session. - Common functions are simplified to single method calls. - Sessions are automatically closed. - Exception are automatically caught and converted to runtime exceptions.|
4. What are the different caching strategies?
|Read-only: This strategy is useful for data that is read frequently but never updated. This is by far the simplest and best-performing cache strategy. Read/write: Read/write caches may be appropriate if your data needs to be updated. They carry more overhead than read-only caches. In non-JTA environments, each transaction should be completed when Session.close() or Session.disconnect() is called. Nonstrict read/write: This strategy does not guarantee that two transactions won't simultaneously modify the same data. Therefore, it may be most appropriate for data that is read often but only occasionally modified. Transactional: This is a fully transactional cache that may be used only in a JTA environment.|
5. What is ORM?
|ORM is an acronym for Object/Relational mapping. It is a programming strategy to map object with the data stored in the database. It simplifies data creation, data manipulation and data access.|
6. What’s the usage of Configuration Interface in hibernate?
|Configuration interface of hibernate framework is used to configure hibernate. It’s also used to bootstrap hibernate. Mapping documents of hibernate are located using this interface.|
7. What type of transaction management is supported in hibernate?
|Hibernate communicates with the database via a JDBC Connection; hence it must support both managed and non-managed transactions.
non-managed in web containers:
8. What is the role of SessionFactory?
|The application obtains session object from SessionFactory interface. Typically there should be only one sessionFacory for whole application and is loaded during application initialization. The SessionFactory caches generate SQL Statement and other mapping metadata that Hibernate use at runtime. It also hold cached data that has been read in one unit of work and can be reused in a future unit of work. You can get the instance of SessionFactory by the configuration object as below SessionFactory sessionFactory = configuration.buildSessionFactory();|
9. What are the two types of collections in hibernate?
|Following are the two types of collections in hibernate: a. Sorted Collection b. Order Collection|
10. How can we reduce database write action times in Hibernate?
|Hibernate provides dirty checking feature which can be used to reduce database write times. Dirty checking feature of hibernate updates only those fields which require a change while keeps others unchanged.|