Hibernate: getReference hands back a proxy that throws after the session closes

getReference (and JpaRepository.getReferenceById) never hits the database. It returns an uninitialised proxy, and the SELECT happens on the first getter - which, outside the transaction, is a LazyInitializationException instead.

Full code & output

This topic is available to PRO members.

Sign in Create a free account

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-25