0

I observe that while saving an entity in database using Hibernate, even if I don't provide a parameter less constructor (there is a parameter constructor provided by me so default constructor can't be provided), it works. But while using the operation delete, it throws an exception that Default constructor is not present. (No default constructor for entity: : com.hibernate.demo.entity.Instructor)

I want to ask in which cases is a parameter less constructor necessary for Hibernate. When is it invoked during a delete operation?

I follwed this question which has some varying answers Why does Hibernate require no argument constructor?

Alok Kumar
  • 41
  • 6
  • 1
    Always. As when retrieving an object JPA (regardless of which implementation) will construct a new instance using the default no-args constructor. Even hibernate does this. It is even in the JPA specification that a no-args constructor for entities is required. The fact tha tyou don't provide one because you have one with arguments doesn't mean you shouldn't add one (it can be package private or private as Hibernate can invoke that as well). – M. Deinum Feb 20 '23 at 10:33
  • 1
    [why do not read official documentation?](https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#entity-pojo). JPA req: **The entity class must have a public or protected no-argument constructor. It may define additional constructors as well**. HBN req: **The entity class must have a no-argument constructor, which may be public, protected or package visibility. It may define additional constructors as well** – Andrey B. Panfilov Feb 20 '23 at 10:52

0 Answers0