Entity class must have no argument constructor

I am learning JPA, so while researching on JPA BLOG Vogella 1.2 Entity Heading I came across a line that reads:

All entity classes must define a primary key, must have a non-arg constructor, and / or cannot be final

I cannot understand this line. Do I need to specifically write a no-argument constructor because the default no-argument constructor is always inherited by classes.

What do they mean without being final, does that mean we can always extend the Entity class? If so, why is it compulsion, why can't it be final


source to share

1 answer

By default, you get a non arg constructor if there is no constructor in your class that has nothing to do with JPA. Yes, if you are defining a constructor, then you need to define an arg constructor, for example:

 public class Student {
      public Student() {}//default ctor which is needed by JPA to convert row from DB to java object
      public Student(int id) {..}//ctor which accepts one arg


Create a final class value that you cannot subclass. A JPA provider like Hibernate creates a proxy for lazy retrieval of strings, which will ultimately limit your performance tuning options. Hence you see that your class may / may not be final depending on your use case.



All Articles