具有默认值的实例变量的 Morphia 投影行为 [英] Morphia projection behavior for instance variable with default value

查看:43
本文介绍了具有默认值的实例变量的 Morphia 投影行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定它是否是 Morphia 的设计方式,但它就是这样......

I'm not sure if its the way Morphia is designed but here it goes...

Student.class(方法省略)

Student.class (methods omitted)

@Entity(value = "students", noClassnameStored = true)
public class Student
{
    @Id
    private String id = new ObjectId().toString();
    private String name;
    private String city = "London"; // Default Value
}

注意:我已将 DEFAULT 值分配给 Instance 变量 city.现在代码...

NOTE: That I have assigned a DEFAULT value to Instance variable city. Now the code...

    Student s1 = new Student("James Bond"); // London IS his default city
    ds.save(s1);

    Student s2 = new Student("Jason Bourne");
    s2.setCity("New York");                 // Set a city for Mr. Bourne
    ds.save(s2);

    System.out.println("----- Normal -----");
    List<Student> studentsNormal = ds.createQuery(Student.class).asList();
    for(Student s: studentsNormal)
        System.out.println(s);

    System.out.println("----- Projected -----");
    Query<Student> query = ds.find(Student.class);
    query.retrievedFields(false, "city");

    List<Student> studentsProjected = query.asList();
    for(Student s: studentsProjected)
        System.out.println(s);

而且,现在输出...

<代码>----- 正常 -----学生{id='57337553db4f0f0f10a93941', name='James Bond', city='London'}学生{id='57337553db4f0f0f10a93942', name='Jason Bourne', city='New York'}----- 预计 -----学生{id='57337553db4f0f0f10a93941', name='James Bond', city='London'}学生{id='57337553db4f0f0f10a93942', name='Jason Bourne', city='London'}

现在,关于 Morphia 行为的 4 个问题...

Now, 4 questions about the Morphia behaviour...

第一季度.对于邦德先生,我没有更改 city 并从投影列表中排除 city,但 city 的默认值打印为伦敦.这不应该是空的吗?

Q1. For Mr. Bond, I did not change the city and excluded city from the projected list, yet the default value of the city is printed as London. Should'nt this be null?

第 2 季度.对于 Bourne 先生,我确实将 city 更改为 New York,但仍然在投影期间,它选择了默认值并显示了 London.这不应该也为空吗?

Q2. For Mr. Bourne, I did change the city to New York but still during the projection, it picked up the default value and showed London. Should'nt this be null as well?

我还没有看到来自 Morphia 的 Projection.class(打算今晚这样做)但似乎对于排除,Morphia 似乎选择了默认值并且不会覆盖 <代码>空.现在这成为一个障碍,因为我没有任何方法可以为我的实例变量提供默认值而不将它们暴露给客户的......

I havent yet seen the Projection.class from Morphia (intend to do so tonight) but it seems that for exclusions, Morphia seems to pick up the default value and does not over-ride with null. Now this becomes a hindrance since I do not have any way to provide default value to my Instance Variables without exposing them to client's...

第三季度.这是 Morphia 还是 MongoD Java 驱动程序行为/问题?

Q3. Is this Morphia or a MongoD Java Driver behavior/issue?

最后一个问题...

第四季度.任何已知的解决方法?

Q4. Any known workaround for this?

我已经谷歌搜索,但到目前为止没有找到任何解决方案或解释......

I have googled around but did not come around any solution or explanation so far...

提前致谢...

SG

推荐答案

当 Morphia 从查询结果中读取您的文档时,它所做的第一件事就是创建您的实体的新实例,Student.它只是调用无参数构造函数.不涉及魔法.城市字段用一个值初始化.完成后,Morphia 将获取从数据库返回的文档中的每个键,找到映射字段并设置它.在您的情况下,文档中没有城市键,因此 Morphia 永远不会设置该字段,而将初始化值保留在原处.

When Morphia is reading your documents from the query results, the first thing it does is create a new instance of your entity, Student. It just invokes the no argument constructor. There's no magic involved. The city field is initialized with a value. Once that's done, Morphia will take each key in the document returned from the database, find that mapped field, and set it. In your case, there is not city key in the document and so that field is never set by Morphia leaving the initialized value in place.

一般来说,像这样初始化实体的字段是一种不好的做法.对于从数据库加载的每个实体,JVM 必须将这些字段初始化为某个值,以便稍后覆盖它们.如果某些字段没有在查询结果中返回,那么在 Morphia 将新实例返回给您的应用程序后,这些值仍然存在.

In general, initializing fields on entities like this is a bad practice. For every entity loaded from the database, the JVM has to initialize those fields to some value only to overwrite them later. In cases such as yours where certain fields don't come back in a query result, those values remain after Morphia returns the new instances back to your application.

这篇关于具有默认值的实例变量的 Morphia 投影行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆