liveBook · Manning

Runtime-generated subclass

As explained in Java Persistence with Spring Data and Hibernate epub

A runtime-generated subclass is a subclass created during program execution to represent or extend an entity type dynamically.

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.

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains proxy creation, class constraints, initialization triggers, type comparison, and explicit loading, but not provider-independent proxy mechanics.
Examples
Demonstrates Item and User references, identifier access, explicit initialization, class inspection, and proxy-backed Bid persistence.
What it assumes
Assumes familiarity with entity mappings and persistence contexts while specifying constructor and non-final-member requirements.
What you can do after
Apply via EntityManager.getReference() and Hibernate inspection APIs while avoiding unnecessary entity loads.
How it connects
Connects entity proxies with lazy loading, persistence contexts, EntityManager, JPA, and Hibernate.

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.