0

I recently learnt that in hibernate, we need a no-arg constructor in an entity because hibernate instantiates its entities via reflection: Hibernate implementation. Are we paying the reflection penalty?

I got curious that whether it is the same case with Spring and found that Spring beans do not require a no-argument constructor mandatorily.

This brings me to the question that how does spring creates its objects if not by reflection - to which I think that Spring is a container and instantiates beans and injects dependencies on startup and it must be able to load the application beans via some classloader and hence it does not need reflection.

Then, I get back to the starting point with the question that hibernate also has my application class definition available then why does it need reflection to create its entities?

can somebody please confirm or correct my understanding and provide me an answer?

Community
  • 1
  • 1
Alok Sharma
  • 31
  • 1
  • 4
  • 3
    Your understanding is wrong, they both use reflection. However when using spring you define which constructor arguments to use this isn't possible for hibernate so hibernate can only call the default constructor and then set fields or properties. – M. Deinum Jul 23 '15 at 05:37
  • Thanks for the course correction, @M.Deinum. – Alok Sharma Jul 23 '15 at 06:03

0 Answers0