A Sdi A Sdi - 1 month ago 11
Java Question

Why the default constructor must be provided in spring-data

I know if I do not implement the default constructor in a spring data an exception will be thrown,actually I faced the same exception when I have to work databases. I never understand the reason. Please advise


First, for clarity, it's not the default constructor (you never explicitly implement the default constructor, that's why it's called a default), it's the zero-parameters constructor.

You need to implement it in most object/relational mappers because they first create the object (with the zero-parameters constructor) and then assign its individual field values to it.

Having said that, if you don't implement any constructors, then the default constructor the compiler will provide should be sufficient for most OR mappers (I haven't used spring-data). By default, it's a zero-parameters constructor with the same accessibility as the class. But if you implement any constructors, the compiler won't create the default for you, and you'd have to supply the zero-parameters constructor yourself.