A runtime-generated subclass acts as a Hibernate proxy for an entity. EntityManager.getReference() can place an Item-shaped placeholder in the persistence context without executing a SELECT; the proxy carries the entity identifier. section 12.1.1
Account for these implementation constraints:
- Keep the entity class and its methods non-final, and provide a public or protected no-argument constructor so Hibernate can generate the subclass. section 12.1.1
- Expect ordinary method calls to initialize the proxy and query the database; an identifier getter may avoid initialization when the identifier is mapped through property access. section 12.1.1
- Do not compare a proxy class directly with Item.class; use HibernateProxyHelper or PersistenceUtil when the represented type or loading state must be inspected. section 12.1.1
Persisting associations without loading entities
Obtain Item and User references with getReference(), assign them to a new Bid, and persist it. Hibernate can issue the BID INSERT without SELECT statements for either associated entity.
