Why JPA Entity or Hibernate Persistence Class Should Not be Final?

Sanjay Singh
Oct 31, 2022

Yes, a Hibernate Entity class can be declared final, however it is not a good practice.

Hibernate uses the proxy pattern for performance improvement during lazy association, by making an entity final, Hibernate will no longer be able to use a proxy as Java doesn’t allow the final class to be extended.

Important points to about Entity class, Final, and Proxying

Hibernate doesn’t create a proxy for final class, instead, they use the real class, but Hibernate does create a proxy for a non-final class with final methods.

3) As per Hibernate documentation, if you’re going to make a class final you should explicitly disable proxy generation by adding @Proxy(lazy=false), but I haven’t noticed any differences between doing that and just making the class final.

--

--

Sanjay Singh

Java||Spring-Boot|| Micro services Application developer|| INDIA