• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Hibernate Interview Questions Answers

Question - 31 : - How can I retrieve the identifier of an associated object, without fetching the association?

Answer - 31 : - Just do it. The following code does not result in any SELECT statement, even if the item association is lazy. Long itemId = bid.getItem().getId(); This works if getItem() returns a proxy and if you mapped the identifier property with regular accessor methods. If you enabled direct field access for the id of an Item, the Item proxy will be initialized if you call getId(). This method is then treated like any other business method of the proxy, initialization is required if it is called.

Question - 32 : - What is a Session? Can you share a session object between different theads?

Answer - 32 : - 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. public class HibernateUtil { public static final ThreadLocal local = new ThreadLocal(); public static Session currentSession() throws HibernateException { Session session = (Session) local.get(); //open a new session if this thread has no session if(session == null) { session = sessionFactory.openSession(); local.set(session); } return session; } } It is also vital that you close your session after your unit of work completes. Note: Keep your Hibernate Session API handy.

Question - 33 : - How can I manipulate mappings at runtime?

Answer - 33 : - You can access (and modify) the Hibernate metamodel via the Configuration object, using getClassMapping(), getCollectionMapping(), etc. Note that the SessionFactory is immutable and does not retain any reference to the Configuration instance, so you must re-build it if you wish to activate the modified mappings.

Question - 34 : - Hibernate uses so much runtime reflection?

Answer - 34 : - Many former C or C++ programmers prefer generated-code solutions to runtime reflection. This is usually justified by reference to the performance red-herring. However, modern JVMs implement reflection extremely efficiently and the overhead is minimal compared to the cost of disk access or IPC. Developers from other traditions (eg. Smalltalk) have always relied upon reflection to do things that C/C++ needs code-generation for. In the very latest versions of Hibernate, "reflection" is optimised via the CGLIB runtime bytecode generation library. This means that "reflected" property get / set calls no longer carry the overhead of the Java reflection API and are actually just normal method calls. This results in a (very) small performance gain.

Question - 35 : - What is the difference between the session.get() method and the session.load() method?

Answer - 35 : - Both the session.get(..) and session.load() methods create a persistent object by loading the required object from the database. But if there was not such object in the database then the method session.load(..) throws an exception whereas session.get(&) returns null.

Question - 36 : - How can I order by the size of a collection?

Answer - 36 : - Use a left join, together with group by select user from User userleft join user.messages msggroup by user order by count(msg)

Question - 37 : - How can I trim spaces from String data persisted to a CHAR column?

Answer - 37 : - Use a UserType.

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners