

So as you are using only non final nullable fields, it may generate an empty constructor even if you don't add the annotation.

This is why you need to add annotation if you add the annotation.Īlso note that you are using which bundles the features of which will generate a constructor for all final or annotated fields (see Lombok documentation). So because the compiler automatically creates a default no-arg constructor when no other constructor is defined, only classes that define constructors must also include a no-arg constructor if required by a framework (here JPA). Then, in Java, a default constructor (no-argument constructor) is automatically generated for a class unless you define other constructors (it only does when you don't provide any other constructor). Which is equivalent to new Product() ( Product.class is a class literal, it can fail at runtime if the class is not found in the classpath), then, once instantiated, uses fields setters to deal with it. Indeed if your class would contain many constructors then JPA wouldn't know which one to call, this is why it instantiates the class through its no-arg constructor using reflections : () This is needed because JPA uses the default constructor method to create a bean class using the reflection API. (note that this is not necessarily true when dealing with some implementation like Hibernate, see this answer). The JPA specification requires that all persistent classes ( have a no-arg constructor, public or protected.
