Why does Hibernate require no argument constructor?
Hibernate, and code in general that creates objects via reflection use Class<T>.newInstance()
to create a new instance of your classes. This method requires a public no-arg constructor to be able to instantiate the object. For most use cases, providing a no-arg constructor is not a problem.
There are hacks based on serialization that can work around not having a no-arg constructor, since serialization uses jvm magic to create objects without invoking the constructor. But this is not available across all VMs. For example, XStream can create instances of objects that don't have a public no-arg constructor, but only by running in a so-called "enhanced" mode which is available only on certain VMs. (See the link for details.) Hibernate's designers surely chose to maintain compatibility with all VMs and so avoids such tricks, and uses the officially supported reflection method Class<T>.newInstance()
requiring a no-arg constructor.
Hibernate and no-arg constructor
There are no special benefits of defining the no-arg constructor explicitly for the Entity Classes (But remember that, Hibernate framework internally uses the no-arg constructor to populate entities through Java reflection API).
It is mandatory that the Hibernate Entity Bean Classes require no-arg constructors, which can be defined explicitly by the programmer (or auto. generated by Java).
One important point is that when you are defining your own constructor(s) for the class, you need to provide the no-arg constructor by yourself (because the compiler does not provide in this case).
Is it mandatory to use no args constructor using @entity annotation in java spring boot?
The creation of schema has nothing to do with the Java constructor. Spring and ORM vendors can read java fields using reflection, and also consider meta-information provided from annotations.
Constructor is just used to create java instances.
I have no constructor at all but when I launch the application and
open the h2 console, I am still able to see the table created from the
entity Person
The table is created from all the info, that I have mentioned above. Nothing to do with constructors
As for your second question
a no-args constructor is needed when we use @entity annotation. Is it
mandatory?
This has to do with how an ORM vendor has made the framework and what requirements he has made for it to work. Specifically for Hibernate yes it is mandatory.
Retrieved from Hibernate Doc
2.2. The entity Java class
- The no-argument constructor, which is also a JavaBean convention, is a requirement for all persistent classes. Hibernate needs to create
objects for you, using Java Reflection. The constructor can be
private. However, package or public visibility is required for runtime
proxy generation and efficient data retrieval without bytecode
instrumentation.
Hibernate no arg constructor
Try to read your event and you will see the exception.
The problem is not in saving object in database when object is already created by explicit call of the constructor. The problem is when hibernate reads record from DB and has to create corresponding object of mapped class. In this case it should create object. How can it do it without default constructor? Indeed there can be several constructor with arguments and it "does not know" which one to take.
So it does something like this:
Object obj = Class.forName(mappedClassName).newInstance();
pupulateProperties(obj);
newInstance()
requires no-args constructor.
Why do we need to declare NOT empty constructor in entity?
for hibernate entity you should provide at least one default constuctor without parameter. If you entity has many fields and just couple are of them are you can create constuctor/constructors with mandatory paremeters but default constructor(without params) should be in entity.
as you Entity it's a normal class you use it not only for hibernate mapping but for other components also.
also you can add static factory method to easy create class.
for exaple :
@Entity
class User{
//mandatory fields
private Long id;
private String userName;
//not mandatory fields:
private Date registrationDate;
private String nickName;
public User(){//it's default constructor for hibernate entity creation
}
public User( Long id ,String userName){ // you can create entity whenever when you want to create entity
....
}
}
with User( Long id ,String userName) - you declare that if you need User instance
you should create it with mandatory field.
in case when you have User() constructor it's hard to understand what parameters are mandatory
Is it possible for Hibernate to use a class with no default c'tor as a component or composite-element?
No, you need a no argument constructor. Hibernate needs a way to create objects.
You might be able to create a subclass of this class, and give the subclass the no-arg constructor.
Why Would a Default Constructor and a Parameterized Constructor Both Be In a Class?
Hibernate requires a no-argument constructor, it uses reflection to call the no-arg constructor when it needs to instantiate a persistent class. So a constructor can be present due to its being needed by a library or framework.
Apparently the other constructor is there as a convenience so the programmer can set the name and ID without calling separate setters.
If you want you can make the no-arg constructor private and Hibernate can still use it.
There is a pattern called constructor chaining (described here: Is it good or bad to delegate to another constructor (using this()) within a constructor) which shows how multiple constructors can make sense, but the code shown isn’t doing that.
Related Topics
How to Generate Random Number in Specific Range in Android
Database Not Copying from Assets
Android Adding Simple Animations While Setvisibility(View.Gone)
Start a Jar File Like Service in Linux
How to Add a Shortcut Key for a Jbutton in Java
Java.Io.Console Support in Eclipse Ide
Why Is String.Chars() a Stream of Ints in Java 8
What Does "Error: '.Class' Expected" Mean and How to Fix It
How to Make a Copy of a File in Android
Android Studio Debugger Highlights the Wrong Lines
Place Cursor at the End of Text in Edittext
How to Debug Java -Jni Using Gdb on Linux
Spring @Transactional - Isolation, Propagation