Morphia List< Map< String,Object>>>返回嵌入式元素不是查找操作上的DBObject [英] Morphia List<Map<String,Object>>> return Embedded element isn't a DBObject on find operation

查看:200
本文介绍了Morphia List< Map< String,Object>>>返回嵌入式元素不是查找操作上的DBObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这样的事情:

package org.dnylabs.kosh.data;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

@Entity
public class Temp {
    @Id String _id;
    List<Map<String,Object>> strings;

    public Temp(){
        strings=new LinkedList<Map<String,Object>>();
    }


    public static void main(String []args) throws UnknownHostException, MongoException{
        Mongo mongo=null;
        Morphia morphia=null;
        Datastore ds=null;
        mongo = new Mongo();
        morphia = new Morphia();
        morphia.map(Temp.class);
        ds = morphia.createDatastore(mongo, "test");
        Temp t = new Temp();
        t._id ="hi";
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("Hi","1");
        m.put("Hi2",2);
        t.strings.add(m);
        ds.save(t);
        t=ds.get(t);
        ds.ensureIndexes();
    }
}

当我尝试做 findAll(9 操作我得到这个例外:

When I try to do a findAll(9 operation I get this exception:

Caused by: java.lang.RuntimeException: org.mongodb.morphia.mapping.MappingException: Embedded element isn't a DBObject! How can it be that is a class java.lang.String
at org.mongodb.morphia.mapping. here`dedMapper.fromDBObject(EmbeddedMapper.java:172)
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:602)
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:559)
at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:256)
at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:203)
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:144)
... 16 more

经过多次尝试,我发现问题是嫁接地图。

After numerous attempts I have found that the problem is the grafted map.

任何人都可以帮助我明白我哪里错了?声明似乎是正确的。

Can anyone help me understand where I'm wrong? The statement seems correct.

推荐答案

Morphia将Map视为对另一个文档的DB引用,而不是将其视为嵌入式类并对待作为文件。解决方案是注释Map @Embedded,但这是不可能的,因为你无法编辑Map类。

Morphia sees Map as a DB reference to another document rather than seeing it as an embedded class and treating as a document. The solution would be to annotate the Map @Embedded, but this is not possible as you can't edit the Map class.

有一种方法可以实现类似于您正在尝试创建另一个类并将Map定义为此类的属性并将其注释为@Embedded。

There is a way to achieve something similar to what you are trying by creating another class and defining the Map as a property of this class and annotate it as @Embedded.

更改Temp类:

public class Temp {
    @Id String _id;

    @Embedded // CHANGE HERE
    List<MapProxy> strings; // CHANGE HERE

    public Temp(){
        strings=new LinkedList<MapProxy>(); // CHANGE HERE
    }

    public static void main(String...args) throws UnknownHostException, MongoException{
        Mongo mongo=null;
        Morphia morphia=null;
        Datastore ds=null;
        mongo = new Mongo();
        morphia = new Morphia();
        morphia.map(Temp.class);
        ds = morphia.createDatastore(mongo, "test2");
        Temp t = new Temp();
        t._id ="hi";      
        MapProxy mp = new MapProxy(); // CHANGE HERE    
        mp.m.put("Hi","1"); // CHANGE HERE
        mp.m.put("Hi2",2); // CHANGE HERE
        t.strings.add(mp); // CHANGE HERE
        ds.save(t);
        t=ds.get(t);
        ds.ensureIndexes();
    }
}

并创建一个新类:

@Embedded
public class MapProxy {
    public Map<String,Object> m = new HashMap<String, Object>();

}

我已经标记了我所做的更改。

I have marked the changes I have made.

这产生的结构如下:

{
    "_id" : "hi",
    "className" : "YOUR CLASS NAME HERE",
    "strings" : 
                [ { 
                     "m" : 
                            { 
                                "Hi" : "1" , 
                                "Hi2" : 2
                            } 
                } ]
}

这篇关于Morphia List&lt; Map&lt; String,Object&gt;&gt;&gt;返回嵌入式元素不是查找操作上的DBObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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