spring-data-mongodb在对象再水化时如何处理构造函数? [英] How exactly does spring-data-mongodb handle constructors when rehydrating objects?
问题描述
我已阅读 http://static.springsource.org/spring-data/data-mongo/docs/1.1.0.RELEASE/reference/html/#mapping-chapter 但无法找到以下基本弹簧的答案-data-mongodb对象映射问题:
I have read http://static.springsource.org/spring-data/data-mongo/docs/1.1.0.RELEASE/reference/html/#mapping-chapter but cannot find the answer to the following basic spring-data-mongodb object mapping question:
如果我从MongoDB加载以下类的实例:
If I load an instance of the following class from MongoDB:
public class Test {
private String str1;
private String str2;
private Date date3;
public Test(String str1) {
this.str1 = str1;
this.date3=new Date();
}
}
我理解构造函数将使用MongoDB文档的顶级字段
。 str1
中找到的值调用Test(String str1)
我假设这个构造函数相当于明确地声明 @PersistenceConstructor
。
I understand that the constructor Test(String str1)
will be invoked with the value found in the top-level field str1
of the MongoDB document.
I assume this constructor is equivalent to declaring a @PersistenceConstructor
explicitly.
但是字段 str2,date3
在这种情况下?是否仍会初始化所有不属于构造函数的字段,或者因为PeristenceConstructor仅使用 str1 str2,date3
值会丢失找到code>?
But what happens to the fields str2, date3
in this case? Will all fields that are not part of the constructor still be initialized, or will the str2, date3
values be lost since a PeristenceConstructor using only str1
was found?
最后,这会以什么顺序发生?将 date3
由构造函数设置,然后被先前持久的字段覆盖,反之亦然?
And lastly, in what order will this happen? Will date3
be set by the constructor, then be overwritten by the previously persisted field, or vice versa?
推荐答案
人口过程是双重的,在某种程度上是正交的。大多数情况下,您已经说明了正确的行为。调用构造函数以创建对象实例。参数值是从 DBObject
读取的,并且可能会导致递归创建对象,以防您将复杂对象移植到需要从嵌套<$ c解组的构造函数中$ c> DBObject 。
The population process is two fold and orthogonal to some degree. Mostly, you've already stated the correct behavior. The constructor is invoked to create an object instance. The parameter values are retrieved from the DBObject
read and might cause a recursive creation of objects in case you hand complex objects into the constructor that need to be unmarshalled from a nested DBObject
.
下一步是填充持久字段。您的情况与默认构造函数的唯一区别在于我们记住您提交给构造函数的字段值,并且不会重新填充它们。
The next step is that your persistent fields get populated. The only difference in your case to the case with a default constructor is that we remember the field values you handed into the constructor and do not re-populate those.
<在构造函数中初始化之后,示例中的code> date 属性仍然会被设置,如果对象具体化的源文档包含<$ c的值$ c>日期。
The date
property in your example would still get set after the initialization in the constructor if the source document the object is materialized from contains a value for date
.
这篇关于spring-data-mongodb在对象再水化时如何处理构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!