Why Jpa Entity Or Hibernate Persistence Shape Should Non Last Final?

One of the interesting hibernate interview questions is, why yous should non brand a Hibernate persistent class final? I'll endeavour to reply this inquiry inwards this brusque weblog post. Use of proxies is the kernel characteristic of Hibernate (one of the most pop ORM framework for Java Projects) for implementing fundamental functioning features e.g. lazy loading as well as lazy associations fetching. In lodge to role a proxy inwards house of existent class, your hibernate persistence class must last either non-final or the implementation of an interface that declares all of Earth methods. Why? because you cannot extend a concluding class inwards Java, as well as to stand upward up equally a proxy, proxy class must satisfy the IS-A relation, which comes either past times extending a class using "extends", or implementing an interface using "implements".

By the way, it doesn't hateful that yous cannot persist your concluding entity class, yous can, but this volition bound Hibernate's mightiness to role proxies for lazy association fetching, which volition impact the functioning of Java application, read Java Persistence alongside Hibernate past times Gavin King, the writer of Hibernate to larn to a greater extent than close this feature.

Hibernate framework makes extensive role or proxy e.g. it uses proxies when yous telephone telephone the load() method, it also uses proxies for lazily loading associations as well as collections of an entity class. Even Hibernate  Reference Manual says "Prefer non-final classes".



Not using proxies is bad for functioning because to a greater extent than SQL than you'd similar may last fired against the database, which also increases database round-trips. You tin verify this past times printing class advert of your entity class as well as hold off at Hibernate query logs, equally shown inwards the next example:


Let's tell yous charge your entity class e.g. a Book, an Employee or a Customer using the load() method as well as farther impress advert of class, yous volition encounter a unlike advert e.g.

Customer client = (Customer) session.load(Customer.class, 1L); logger.info("Name of Customer Entity class : " + customer.getClass());

volition impress "sample.hibernate.Customer_$$_javassist_0" which is a JavaAssist generated the proxy.


You volition non encounter whatever guide query fired because the charge is lazy as well as it volition non charge object until yous telephone telephone whatever method other than the getId(), equally seen inwards the difference betwixt the get() vs load() inwards Hibernate article.

Now, if yous brand the class concluding as well as reprint the advert of the class, yous volition encounter the actual advert of the class equally "Customer". You will also encounter the guide queries fired past times hibernate to initialize the object.

You should right away empathize how costly it tin last if yous are creating also many objects as well as alone accessing their getId() method, equally it volition termination inwards lots of database calls. That's why yous should non brand your JPA Entity or Hibernate persistence class concluding inwards Java. Let's encounter about to a greater extent than points inwards adjacent section.

final methods.

2) Since inwards Java, yous cannot override concluding methods, the code within concluding method stay unchanged inwards the proxy class. Which means, if yous telephone telephone a concluding method on a proxy, it volition only delegate the telephone telephone to the super class method. You should last careful non to modify country on concluding method because calling them on proxy has a proficient peril of throwing NullPointerException, equally the object was probable to last non initialized at that point.

In short, avoid concluding methods on hibernate entity class, until yous know what yous are doing. Hibernate reference also stress this betoken "You should  avoid declaring world concluding methods equally this volition ane time again bound the mightiness to generate proxies from this class. If yous desire to role a class alongside world concluding methods, yous must explicitly disable proxying".

3) As per Hibernate documentation, if  you're going to make a class final yous should explicitly disable proxy generation past times adding @Proxy(lazy=false), but I haven't noticed whatever differences betwixt doing that as well as exactly making the class final.

4) You should also disable proxy generation if you're going to brand whatever world methods concluding inwards persistent class, equally hibernate volition non last able to override them to set its smart code which triggers lazy loading etc.

5) If yous actually desire to brand the Hibernate entity classes concluding as well as then yous tin exercise it past times having your entity implement an interface that declares all of its world methods. This volition all the same permit Hibernate to role proxies.

In short, making a JPA Entity or Hibernate Persistence class final, limits the mightiness of Hibernate to role Proxies, which inwards plough preclude Hibernate from applying about functioning optimizations. Without proxies, your application loses lazy loading, as well as lazy association fetching will number to a greater extent than SQL queries as well as brand to a greater extent than database roundtrip, which volition toll performance.

The alone way to brand a Hibernate entity class concluding without losing lazy behaviour is to role interface as well as define all world methods of persistence class there, this volition all the same permit Hibernate to role a proxy inwards house of existent class, encounter Java Persistence alongside hibernate to a greater extent than details.

why yous should non brand a Hibernate persistent class concluding Why JPA Entity or Hibernate Persistence Class Should Not last Final?



Related Hibernate as well as Spring Interview Questions
  • Difference betwixt save(), persist() as well as saveOrUpdate() inwards Hibernate? (answer)
  • Difference betwixt First as well as Second score cache inwards Hibernate? (answer)
  • Difference betwixt get() as well as load() method inwards Hibernate? (answer)
  • What should yous recollect piece overriding equals() as well as hashCode for Hibernate entity class?(answer)
  • Why default or no-argument constructor is of import inwards Java? (answer)
  • When should yous role setter as well as constructor injection inwards Spring? (answer)
  • What is the default edible bean compass inwards Spring framework? (answer)
  • What is the divergence betwixt BeanFactory as well as ApplicationContext inwards Spring? (answer)


Further Learning
Introduction To Hibernate
Spring as well as Hibernate for Beginners
Hibernate Interview Questions Preparation Course

0 Response to "Why Jpa Entity Or Hibernate Persistence Shape Should Non Last Final?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel