Java 实体 - 为什么我需要一个空的构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18099127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java entity - why do I need an empty constructor?
提问by sliders_alpha
This might sound stupid to you,
but why do I need to define an empty constructor in my @Entity
s?
这对你来说可能听起来很愚蠢,但为什么我需要在我的@Entity
s 中定义一个空的构造函数?
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).
但是 Java 总是给你一个默认的不可见的空构造函数(如果你不重新定义一个)。
Thanks.
谢谢。
EDIT
编辑
I think there is a semantic problem. 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).
但是当您不重新定义它时(编写另一个带参数的构造函数),Java 总是为您提供这个空构造函数。
In this case writing this empty constructor seems useless.
在这种情况下,编写这个空构造函数似乎毫无用处。
采纳答案by u6f6o
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 注释,如下所示
@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.
如果您的项目中存在 Spring Data,则初始化您的实体。因此,您也可以避免使用空构造函数。
回答by Kevin Bowersox
Explicitly defining a default constructor is not necessary unless you provide another constructor for the entity. If you provide another constructor, aside from one with the default constructor's signature, the default constructor will not be created.
除非您为实体提供另一个构造函数,否则不需要显式定义默认构造函数。如果您提供另一个构造函数,除了具有默认构造函数签名的构造函数之外,将不会创建默认构造函数。
Since JPA implementations rely upon the existence of a default constructor it is then necessary to include the default constructor that will be omitted.
由于 JPA 实现依赖于默认构造函数的存在,因此有必要包含将被省略的默认构造函数。
回答by Juned Ahsan
But java always give you a default invisible empty constructor (if you don't redefine one).
但是java总是给你一个默认的不可见的空构造函数(如果你不重新定义一个)。
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.
仅当您在类中未提供任何构造函数时,此语句才是正确的。如果您的类中提供了参数构造函数,那么 jvm 将不会添加无参数构造函数。
回答by Maxim Shoustin
Actually you don't need to write it. You have it by default. Sometimes you can create private
constructor to prevent users to use default
其实没必要写。默认情况下,您拥有它。有时您可以创建private
构造函数来防止用户使用默认值
public class MyClass{
private MyClass(){}
}
For singelton patterns, for example you can block using default constructor.
例如,对于单例模式,您可以使用默认构造函数进行阻塞。
Sometimes, when you use Gson
plugin to convert String Json data to Object, it demands to write default constructor, otherwise it doesn't work
有时,当你使用Gson
插件将String Json数据转换为Object时,需要编写默认构造函数,否则不起作用
回答by JavaNewbie_M107
From the JPA tag, I suppose that you are working with Java beans. Every bean needs to have the following properties:
根据 JPA 标记,我假设您正在使用 Java bean。每个 bean 都需要具有以下属性:
Getters and setters for all its main instance variables.
An empty constructor.
All its instance variables should preferably be
private
.
其所有主要实例变量的 getter 和 setter。
一个空的构造函数。
它的所有实例变量最好是
private
.
Thus the statement : "every entity needs an empty constructor".
因此声明:“每个实体都需要一个空的构造函数”。
回答by gjman2
Java not always give you a default invisible empty constructor if your class got argument constructor, you have to define the empty constructor by your own.
如果你的类有参数构造函数,Java 并不总是给你一个默认的不可见的空构造函数,你必须自己定义空构造函数。
回答by Arnaud Denoyelle
As you specified the "JPA" tag, I assume your question applies to JPA only and not empty constructors in general.
当您指定“JPA”标签时,我假设您的问题仅适用于 JPA,而不适用于一般的空构造函数。
Persitence frameworks often use reflection and more specifically Class<T>.newInstance()
to instantiate your objects, then call getters/setters by introspection to set the fields.
持久性框架通常使用反射,更具体地说,Class<T>.newInstance()
用于实例化您的对象,然后通过自省调用 getter/setter 来设置字段。
That is why you need an empty constructor and getters/setters.
这就是为什么您需要一个空的构造函数和 getter/setter 的原因。
See this StackOverflow question about empty constructors in Hibernate.