Using the Hibernate mailing list (thanks to Emmanuel Bernard!) I can answer my own question: summary: The final methods do not prevent Hibernate from creating a proxy server at all, but if these methods do not use any state of the entity, it is extremely impractical.
Some background information: Hibernate does not use bytecode amplification with either cglib or Javassist, so for a proxy to initialize its target entity lazily, it must intercept any method that can use the state of this target. Now it’s perfectly normal to have a final method like this
public final doSomething(String a, Integer b ) {
but as soon as this method uses some constant field directly or through another instance method , it will bypass the proxy server and thus lead to unexpected behavior.
As a side element, this is the same reason why you should not directly access the fields of other instances, for example, in equals entity methods:
// XXX bad code! public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Profile)) return false; Profile profile = (Profile) o; // XXX this bypasses a possible proxy, use profile.getName() instead! return (name == null ? profile.name == null : name.equals(profile.name)); }
Robin source share