Two Laptop Bag*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Doubt in Sun's sample question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Doubt in Sun Watch "Doubt in Sun New topic
Author

Doubt in Sun's sample question

Minu Jain
Ranch Hand

Joined: Mar 24, 2008
Posts: 74
Following is a question from Sun's site: webpage



7) Which statement is correct about the EntityManager API?
a) The merge, persist, remove, and getReference methods must be invoked within a transaction context.
b) It is safe (no exception is thrown) to call merge or getTransaction on a JTA EntityManager instance.
c) The getReference method can throw an EntityNotFoundException. (*)
d) Runtime exceptions thrown by the refresh and createQuery methods of the EntityManager interface do NOT cause the transaction to be rolled back.

REFERENCE:
Option A is incorrect. getReference does NOT need to be invoked within a transaction context. (See JPA Specification 3.1.1.)
Option B is incorrect. An exception is thrown if you call getTransaction on a JTA transaction manager. (See the JPA Specification 3.1.1 interface listing.)
Option C is correct. Even though in general this behaves as a lazy proxy to an entity, an EntityNotFoundException can be thrown. (See the JPA Specification 3.1.1 interface documentation.)
Option D is incorrect. It causes the exception to roll back. (See JPA Specification 3.1.1.)



Here is my doubt:
I checked the JPA specification 3.1.1 and it says this about getReference method:


public <T> T getReference(Class<T> entityClass, Object primaryKey);
/**
* Synchronize the persistence context to the
* underlying database.
* @throws TransactionRequiredException if there is
* no transaction
* @throws PersistenceException if the flush fails
*/


So, shouldn't option a be correct???
Please help..............
Thanks


SCBCD5, SCWCD5, SCJP5
"Even if you're on the right track, you'll get run over if you just sit there."
Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
Actually...
You are looking at the wrong place...


@throws PersistenceException if the flush fails


which means...you are looking at the "Flush" method, not at the "getReference"...

The Specification for the "Flush" is right after the "getReference" so i guess you just
thought the specified method is followed but the details...but is the opposite, in the
specification, first you find the explanation and then the method.

getReference Method


/**
* Get an instance, whose state may be lazily fetched.
* If the requested instance does not exist in the database,
* the EntityNotFoundException is thrown when the instance
* state is first accessed. (The persistence provider runtime is
* permitted to throw the EntityNotFoundException when
* getReference is called.)
* The application should not expect that the instance state will
* be available upon detachment, unless it was accessed by the
* application while the entity manager was open.
* @param entityClass
* @param primaryKey
* @return the found entity instance
* @throws IllegalArgumentException if the first argument does
* not denote an entity type or the second
* argument is not a valid type for that
* entity’s primary key
* @throws EntityNotFoundException if the entity state
* cannot be accessed
*/
public <T> T getReference(Class<T> entityClass, Object primaryKey);




flush Method


/**
* Synchronize the persistence context to the
* underlying database.
* @throws TransactionRequiredException if there is
* no transaction
* @throws PersistenceException if the flush fails
*/
public void flush();


Therefore, option (C) is correct ;)

Cheers,
Dave


SCJP, SCBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Sun's sample question