Java Spring MCQs on “Hibernate and JPA’s Contextual Sessions”.
1. Spring HibernateTemplate can simplify your DAO implementation by managing sessions and transactions for you.
a) True
b) False
Answer: a
Clarification: However, using HibernateTemplate means your DAO has to depend on Spring API.
2. An alternative to Spring HibernateTemplate is:-
a) HibernateContext
b) Hibernate contextual sessions
c) All of the mentioned
d) None of the mentioned
Answer: b
Clarification: An alternative to Spring HibernateTemplate is to use Hibernate contextual sessions.
3. Sessionfactory can manage contextual sessions for you and allows you to retrieve them by the:-
a) getSession() method
b) getCurrent() method
c) getCurrentSession() method
d) none of the mentioned
Answer: c
Clarification: In Hibernate 3, a sessionfactory can manage contextual sessions for you and allows you to retrieve them by the getCurrentSession() method on org.hibernate.SessionFactory.
4. DAO methods require access to the session factory, which can be injected:-
a) a setter method
b) constructor argument
c) none of the mentioned
d) all of the mentioned
Answer: d
Clarification: To use the contextual session approach, your DAO methods require access to the session factory, which can be injected via a setter method or a constructor argument.
5. DAO methods must be made transactional.
a) True
b) False
Answer: a
Clarification: This is required because Spring wraps the SessionFactory with a proxy that expects that Spring transaction management is in play when methods on a session are made.
6. Annotation to find a transaction and then fail, complaining that no Hibernate session been bound to the thread.
a) @Transaction
b) @Transactional
c) @Transactions
d) None of the mentioned
Answer: b
Clarification: This ensures that the persistence operations within a DAO method will be executed in the same transaction and hence by the same session.
7. In the bean configuration file for Hibernate (i.e., beans-hibernate.xml), you have to declare a HibernateTransactionManager instance for this application and enable declarative transaction via:-
a) tx:annotation
b) tx:annotationdriven
c) tx:annotation-driven
d) none of the mentioned
Answer: c
Clarification: In the bean configuration file for Hibernate (i.e., beans-hibernate.xml), you have to declare a HibernateTransactionManager instance for this application and enable declarative transaction management via tx:annotation-driven.
8. HibernateTemplate will translate the native Hibernate exceptions into exceptions in Spring DataAccessException hierarchy.
a) True
b) False
Answer: a
Clarification: This allows consistent exception handling for different data access strategies in Spring.
9. Annotation for Hibernate exceptions to be translated into Spring DataAccessException for consistent exception handling:-
a) @Translation
b) @Repo
c) @Repository
d) None of the mentioned
Answer: c
Clarification: If you want the Hibernate exceptions to be translated into Spring DataAccessException for consistent exception handling, you have to apply the @Repository annotation to your DAO class that requires exception translation.
10. Instance to translate the native Hibernate exceptions into data access exceptions in Spring DataAccessException hierarchy.
a) PersistenceExceptionPostProcessor
b) PersistenceExceptionTranslation
c) PersistenceException
d) PersistenceExceptionTranslationPostProcessor
Answer: d
Clarification: This bean post processor will only translate exceptions for beans annotated with @Repository.
11. You can assign a component name in this annotation and have the session factory autowired by the Spring IoC container with @Autowired.
a) True
b) False
Answer: a
Clarification: In Spring, @Repository is a stereotype annotation. By annotating it, a component class can be auto-detected through component scanning.
12. Spring provides to simplify your DAO implementation by managing entitymanagers and transactions for you:-
a) HibernateTemplate
b) JpaTemplate
c) SpringTemplate
d) None of the mentioned
Answer: b
Clarification: Using Spring JpaTemplate means your DAO is dependent on Spring API.
13. Annotation used for entity manager injection in EJB components.
a) @PersistenceContext
b) @Persistence
c) @PersistenceCon
d) None of the mentioned
Answer: a
Clarification: Spring can also interpret this annotation by means of a bean post processor.
14. To use the context injection approach, you can declare an entity manager field in your DAO and annotate it with the @PersistenceContext annotation.
a) True
b) False
Answer: a
Clarification: Spring will inject an entity manager into this field for you to persist your objects.
15. JpaTemplate will translate the native JPA exceptions into exceptions in Spring DataAccessException hierarchy.
a) True
b) False
Answer: a
Clarification: If you want JPA exceptions to be translated into Spring DataAccessException, you have to apply the @Repository annotation to your DAO class.