liveBook · Manning

getReference

As explained in Java Persistence with Spring Data and Hibernate epub

getReference is a JPA EntityManager operation that returns a reference to an entity, commonly without immediately retrieving its database state.

JPA’s EntityManager.getReference() returns an entity reference without requiring an immediate SELECT. When the persistence context already manages the identifier, it returns that instance; otherwise Hibernate creates an uninitialized proxy carrying the identifier. Unlike find(), it does not return null for a missing database row. section 10.2.4 section 12.1.1

Use the reference with these lifecycle and initialization rules:

  • Call the mapped identifier getter without necessarily initializing the proxy; calling another method, such as getName(), triggers a SELECT. section 10.2.4 section 12.1.1
  • Check initialization with PersistenceUnitUtil.isLoaded() or Hibernate.isInitialized(), and explicitly load data with Hibernate.initialize() while the persistence context remains open. section 10.2.4 section 12.1.1
  • Accessing an uninitialized proxy after the persistence context closes causes LazyInitializationException; initializing it beforehand avoids that failure. section 10.2.4
  • A missing row is reported as EntityNotFoundException when the proxy initializes, rather than during getReference() itself. section 10.2.4
  • Entity classes must support Hibernate proxying: they need a public or protected no-argument constructor and non-final classes and methods. section 12.1.1

Persisting an association without loading either entity

Item item = em.getReference(Item.class, ITEM_ID); User user = em.getReference(User.class, USER_ID); Bid bid = new Bid(new BigDecimal("99.00")); bid.setItem(item); bid.setBidder(user); em.persist(bid); This procedure can issue only the BID INSERT, with no SELECT for the referenced Item or User. section 12.1.1

Path: managing-data/src/test/java/com/manning/javapersistence/ch10
 /SimpleTransitionsTest.java – retrievePersistentReference()
 
Item item = em.getReference(Item.class, ITEM_ID);                         #1
PersistenceUnitUtil persistenceUtil =                                     #2
    emf.getPersistenceUnitUtil();
assertFalse(persistenceUtil.isLoaded(item));                              #3
// assertEquals("Some Item", item.getName());                             #4
// Hibernate.initialize(item);                                            #5
em.getTransaction().commit();
em.close();                                                               #6
assertThrows(LazyInitializationException.class, () -> item.getName());    #7
#1 The persistence context.
#2 The helper methods.
#3 Detecting an uninitialized proxy.
#4 Mapping the exception to the rule.
#5 Load the proxy data.
#6 <kbd>item</kbd> is in a detached state.
#7 Load data after closing the persistence context.

Follow the executable lifecycle example from proxy creation and isLoaded() verification through persistence-context closure and the resulting lazy-access exception. section 10.2.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains proxy creation, initialization triggers, missing-row errors, detachment failures, proxy requirements, and explicit loading.
Examples
Demonstrates Item and User references, Bid persistence, isLoaded() checks, identifier access, explicit initialization, and detached-proxy failure.
What it assumes
Assumes basic JPA entity and persistence-context knowledge while teaching proxy state, initialization timing, and detached access consequences.
What you can do after
Apply it via EntityManager to assign associations efficiently, inspect proxy state, and initialize required data before detachment.
How it connects
Connects getReference() to EntityManager.find(), persistence contexts, proxies, lazy loading, transactions, and entity lifecycle states.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.