persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Add an EntityManager#getReference() method that takes an ID and a version

Open lukasj opened this issue 11 years ago • 4 comments

Please consider adding an EntityManager#getReference(Class, Object, Object) method to the JPA specification.

The method's purpose should be to return an entity provided that its identifier and version are equal to the supplied parameters.

It should be possible to pass null as the third parameter (version).

The javadoc could read something like the following:

/* 
 * Get an instance, whose state may be lazily fetched, provided that the
 * supplied class, identifier and version parameters collectively identify the
 * persistent representation of the entity in question.
 *
 * <p>If the requested instance does not exist in the database, an 
 * {@link EntityNotFoundException} is thrown when the instance state
 * is first accessed. (The persistence provider runtime is permitted to throw
 * the {@link EntityNotFoundException} when {@code getReference} is
 * called.)</p>
 * 
 * <p>The application should not expect that the instance state will be  
 * available upon detachment, unless it was accessed by the application while 
 * the {@link EntityManager} was open.</p>
 *
 * @param entityClass the {@link Class} of the entity being sought; must not
 * be {@code null}
 * 
 * @param primaryKey the identifier of the entity being sought; must not be
 * {@code null}
 *
 * @param version the version of the entity being sought; may be {@code
 * null} in which case this method will behave in exactly the same manner as
 * the {@link #getReference(Class, Object)} method
 *
 * @return the found (non-{@code null}) entity reference
 *
 * @exception IllegalArgumentException if {@code entityClass} does not 
 * denote an entity type or {@code primaryKey} is not a valid type for that
 * entity's primary key or is {@code null}
 *
 * @exception EntityNotFoundException if the entity state cannot be accessed
 */

The background for this issue can be accessed in the following email thread: https://java.net/projects/jpa-spec/lists/users/archive/2014-06/message/0

lukasj avatar Jul 01 '14 23:07 lukasj

  • Issue Imported From: https://github.com/javaee/jpa-spec/issues/82
  • Original Issue Raised By:@glassfishrobot
  • Original Issue Assigned To: @ldemichiel

lukasj avatar Aug 31 '18 16:08 lukasj

@glassfishrobot Commented Reported by @ljnelson

lukasj avatar Jul 01 '14 23:07 lukasj

@glassfishrobot Commented This issue was imported from java.net JIRA JPA_SPEC-82

lukasj avatar May 05 '17 06:05 lukasj

This is actually a good idea, and one that has never occurred to me before. 👍

gavinking avatar Aug 09 '23 17:08 gavinking