Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Hibernate Interview Questions and Answers

Test your skills through the online practice test: Hibernate Quiz Online Practice Test

Related differences

Ques 41. What is a SessionFactory? Is it a thread-safe object?

SessionFactory is Hibernates concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in an application code.

Is it helpful? Add Comment View Comments
 

Ques 42. What is a Session? Can you share a session object between different threads?

Session is a light weight and a non-threadsafe object (No, you cannot share it between threads) that represents a single unit-of-work with the database. Sessions are opened by a SessionFactory and then are closed when all work is complete. Session is the primary interface for the persistence service. A session obtains a database connection lazily (i.e. only when required). To avoid creating too many sessions ThreadLocal class can be used as shown below to get the current session no matter how many times you make call to the currentSession() method.

Is it helpful? Add Comment View Comments
 

Ques 43. What are the benefits of detached objects?

Detached objects can be passed across layers all the way up to the presentation layer without having to use any DTOs (Data Transfer Objects). You can later on re-attach the detached objects to another session.

Is it helpful? Add Comment View Comments
 

Ques 44. What are the pros and cons of detached objects?

Pros:
" When long transactions are required due to user think-time, it is the best practice to break the long transaction up into two or more transactions. You can use detached objects from the first transaction to carry data all the way up to the presentation layer. These detached objects get modified outside a transaction and later on re-attached to a new transaction via another session.

Cons
" In general, working with detached objects is quite cumbersome, and better to not clutter up the session with them if possible. It is better to discard them and re-fetch them on subsequent requests. This approach is not only more portable but also more efficient because - the objects hang around in Hibernate's cache anyway.

" Also from pure rich domain driven design perspective it is recommended to use DTOs (DataTransferObjects) and DOs (DomainObjects) to maintain the separation between Service and UI tiers.

Is it helpful? Add Comment View Comments
 

Ques 45. How does Hibernate distinguish between transient (i.e. newly instantiated) and detached objects?

" 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().

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook
Hibernate 3 vs Hibernate 4JDBC vs HibernateJPA vs Hibernate