Difference between Emp e = session.load(Emp.class) and Emp e = session.createCriteria(Emp.class) ?

  1. a) On session close session.load() Emp Object unavailable to access

  2. b) On session close session.createCriteria() Emp unavailable to access

  3. c) None

  4. Both a & b


Correct Option: A
Explanation:

To understand the difference between session.load(Emp.class) and session.createCriteria(Emp.class), the user needs to know about Hibernate, an ORM (Object-Relational Mapping) framework that provides a way to map Java objects to database tables.

session.load(Emp.class) and session.createCriteria(Emp.class) are two different ways to retrieve data from the database using Hibernate.

session.load(Emp.class) is used to load an object from the database using its identifier. It returns a proxy object of the requested entity and the actual database query is executed only when the proxy object is accessed for the first time. If the identifier is not found in the database, then it throws the ObjectNotFoundException exception.

On the other hand, session.createCriteria(Emp.class) is used to create a Criteria object that can be used to retrieve data from the database. It provides a way to construct a query using a set of criteria such as restrictions, projections, and ordering. It returns a list of objects that match the criteria specified in the query.

Now let's go through each option and explain why it is right or wrong:

A. a) On session close session.load() Emp Object unavailable to access: This option is partially correct. When the session is closed before accessing the Emp object loaded using session.load(), then the ObjectNotFoundException exception is thrown. However, the Emp object is still available to access until the session is closed.

B. b) On session close session.createCriteria() Emp unavailable to access: This option is incorrect. When the session is closed before accessing the Emp object retrieved using session.createCriteria(), then the LazyInitializationException exception is thrown. However, the Emp object is still available to access until the session is closed.

C. c) None: This option is incorrect. There is a difference between session.load() and session.createCriteria() as explained above.

D. Both a & b: This option is incorrect. Option A is partially correct and option B is incorrect.

Therefore, the correct answer is:

The Answer is: A

Find more quizzes: