Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Doubt in Sun's sample question

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic