Search your topic in Java2share
Home > SCBCD > SCBCD Question Bank
eXTReMe Tracker

7. Persistence Units and Persistence Contexts (SCBCD)

Q: 1 A developer wants to create a portable EJB 3.0 application that includes the
following class definition for the Entity Account:
11. @Entity
12. @EntityListeners(com.acme.AlertMonitor.class)
13. public class Account {
14. // more code here
15. @PrePersist
16. protected void validateCreate() {/* more code here */}
17. }
Which statement is correct?

A. The validateCreate method may NOT throw runtime exceptions.
B. The validateCreate method can invoke the EntityManager.flush operation.
C. Methods of the class com.acme.AlertMonitor annotated with callback annotations must take an Object or
Account instance as the only argument.
D. The above class definition is NOT correct. An entity cannot define a callback method like PrePersist and
use the EntityListeners annotation at the same time.

Answer: C


Q: 2 Given:
11. @PersistenceContext EntityManager em;
12. public boolean test(Order o) {
13. boolean b = false;
14. o = em.merge(o);

15 em.remove(o);
16. o = em.merge(o);
17. b = em.contains(o);
18. return b;
19. }
Which statement is correct?

A. The method will return TRUE.
B. The method will return FALSE.
C. The method will throw an exception.
D. The Order instance will be removed from the database.

Answer: C

 Q: 03 Which is a valid way of injecting a container-managed transaction-scoped
persistence context into an EJB 3.0 session bean assuming the application contains only one persistence

A. @PersistenceUnit
public EntityManager em;
B. @PersistenceContext
private EntityManager em;
C. @TransactionManagement(TransactionManagementType.CONTAINER)
public EntityManager em;
D. @Resource(name="persistence/em", authenticationType=AuthenticationType.CONTAINER)
protected EntityManager em;

Answer: B

 Q: 04 A developer writes an enterprise application and packages it into an
enterprise archive (EAR). The application contains one persistence unit with unit name "FooPU". The
application also contains an ejb-jar with one stateless session bean. Which code, when added to the
stateless session bean class, injects a container-managed persistence context at runtime?

A. @PersistenceContext
EntityManager em;
B. @Resource(unitName="FooPU")
EntityManager em;
C. @PersistenceUnit(unitName="FooPU")
EntityManager em;
D. @PersistenceContext(unitName="persistence/FooPU")
EntityManager em;

Answer: A