Perfect for setting a foreign key when you only have the id and never read the object.
// Needs the whole customer row:
Order o = new Order();
o.setCustomer(em.find(Customer.class, 7L)); // SELECT
// Needs only the FK value:
o.setCustomer(em.getReference(Customer.class, 7L)); // no SELECT
find: select ... from customers where id=7
getReference: (no query) then insert into orders (customer_id) values (7)
Touching any field of the proxy triggers the select after all.
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-11