Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java entity - why do I need an empty constructor?

Tags:

java

jpa

entity

This might sound stupid to you, but why do I need to define an empty constructor in my @Entitys?

Every tutorial I saw said : every entity needs an empty constructor.

But Java always give you a default invisible empty constructor (if you don't redefine one).

Let me clarify.. What I understood by "need" was write.

Meaning: always write an empty constructor in your entity.

example:

@Entity public class MyEntity implements Serializable {     @Id    private String str;     public MyEntity(){}     //here getter and setter } 

But Java always gives you this empty constructor when you don't redefine it (write an other one with parameters).

In this case writing this empty constructor seems useless.

like image 908
sliders_alpha Avatar asked Aug 07 '13 09:08

sliders_alpha


People also ask

Why do we need empty constructors in Java?

An empty constructor is required when we need to create a new instance via reflection by our framework. If we don't create any other constructor with arguments for the class, we don't need to create an empty constructor because one default will already be present.

Why does JPA need empty constructor?

JPA provider needs to create instances of entity. If class would contain only constructor which takes arbitrary arguments, JPA provider cannot figure out values for those arguments. Probably public visibility is simply not aloud because fields should not be directly accessed from outside of class.

Why do we need a default constructor in entity class?

Reason: Default constructor is required to have Hibernate initialize the entity; private is allowed but package private (or public) visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation.

Do you always need a constructor in Java?

Java doesn't require a constructor when we create a class. However, it's important to know what happens under the hood when no constructors are explicitly defined. The compiler automatically provides a public no-argument constructor for any class without constructors. This is called the default constructor.


2 Answers

An empty constructor is needed to create a new instance via reflection by your persistence framework. If you don't provide any additional constructors with arguments for the class, you don't need to provide an empty constructor because you get one per default.

You can also use the @PersistenceConstructor annotation which looks like following

@PersistenceConstructor public Movie(Long id) {     this.id = id; } 

to initialise your entity if Spring Data is present in your project. Thus you can avoid the empty constructor as well.

like image 83
u6f6o Avatar answered Sep 29 '22 10:09

u6f6o


But java always give you a default invisible empty constructor (if you don't redefine one).

This statement is true only when you don't provide any constructor in your class. If an argument constructor is provided in your class, then jvm will not add the no-argument constructor.

like image 25
Juned Ahsan Avatar answered Sep 29 '22 10:09

Juned Ahsan