Ans: This getCurrentSession() method allows the session bound to come back to the context. If you want it to function properly, you need to configure it in Hibernate configuration file. The session object is possessed by the context of Hibernate. You don’t need to close it as it closes with the SessionFactory.
Ans:To provide support to Lazy loading, Hibernate makes use of a proxy object. When you request to load an object, it is transferred from the database. It comes with a reference to another concrete object, so instead of that concrete associated object, Hibernate returns a proxy.
Ans: Most of the developers who take this test find this question tough. Even though the answer to it is very straightforward and consists of a few lines, it may put you in a tricky situation. To save you from being clueless in your interview, here is when to use the merge and update in hibernate.
Update(): You use it when you are certain that the Hibernate Session does not have a persistent instance already with the same id.
merge(): Without knowing about the state of the Session, you can use it to merge your modifications whenever you want.
Ans: This is undoubtedly one of the most asked questions during the Hibernate Interview. That is why we only tell you the primary difference between the get() and load() methods.
load(): Suppose, an object is being passed to them has an unidentified ID, it will throw an exception.
get(): is going to return null.
load(): It can easily return the proxy without going to the database if it isn’t necessary.
get(): It must hit the database.
Thus, in some cases, load() can be more rapid as compared to the get() method.
Ans: In the Transient state, in the Java program, the formation of new objects takes place. They are not related to any Hibernate Session. You may have guessed the object that is involved in this state. In the Persistent state, the object is associated with a Hibernate session. This object is famous as the Persistent object.
The object that has prior associated with Hibernate session, but now it’s not associated is called a detached object. If you want to store these objects in the database, you can call the save() or persist() method and change them into the Persistent state.
In the Detached state, you can call either update() or saveOrUpdate() method and attach the object again to Hibernate sessions.
|