Entity Class Must Not Be Final
ID |
java.hibernate_entity_not_final |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
best-practice, hibernate, orm, reliability |
Description
Reports @Entity classes declared final. Hibernate generates runtime proxies by subclassing entity classes to support lazy loading and dirty checking. A final class cannot be subclassed, causing Hibernate to either fail at startup or silently disable proxying.
Rationale
Hibernate relies on bytecode generation (CGLIB or Byte Buddy) to create proxy subclasses for entities. When an entity class is declared final, proxy creation fails and lazy-loading associations degrade to eager fetching, defeating the purpose of the ORM’s performance optimizations.
// Bad: final entity cannot be proxied
@Entity
public final class Order {
@Id
private Long id;
private String description;
}
Remediation
Remove the final modifier from the entity class. If immutability is desired, make individual fields final or use defensive copying in setters instead of sealing the class hierarchy.
// Good: non-final entity allows proxying
@Entity
public class Order {
@Id
private Long id;
private String description;
}