如何将log4mongo Document映射到java对象

时间:2012-01-17 14:54:37

标签: mongodb log4j morphia

基本上我的log4mongo正在记录用户活动,我想选择 一个用户并查看他的日志条目,

此代码正常运行

BasicDBObject blogEntryDbObj = (BasicDBObject) 
mongo.getDB().getCollection("log").findOne(new BasicDBObject("_id", 
new ObjectId(blogEntryId))); 

但是我在这里得到错误“试图映射到未映射的类:

log = morphia.fromDBObject(log.class, blogEntryDbObj); 

package类的log名称应该是什么? 是无关紧要的。我意识到可能我的POJO没有正确设置
但我无法弄清楚出了什么问题。

log4mongo文档看起来像这样。

{ 
  "_id" : ObjectId("4eecf4e4f9cb90f308193614"), 
  "timestamp" : new Date("Sat, 17 Dec 2011 21:00:36 GMT +01:00"), 
  "level" : "INFO", 
  "thread" : "Server.Connection:146.53.243.167:4570", 
  "message" : "setting RegistrationId for: zambe", 
  "loggerName" : { 
    "fullyQualifiedClassName" : "zambe.afa8cc37-4796-49c1- 
a175-05a8daf0a88c", 
    "package" : ["zambe", "afa8cc37-4796-49c1-a175-05a8daf0a88c"], 
    "className" : "afa8cc37-4796-49c1-a175-05a8daf0a88c" 
  }, 
  "fileName" : "PushUpdateRegistrationId.java", 
  "method" : "connect", 
  "lineNumber" : "61", 
  "class" : { 
    "fullyQualifiedClassName" : 
"com.carlsberg.server.PushUpdateRegistrationId", 
    "package" : ["com", "carlsberg", "server", 
"PushUpdateRegistrationId"], 
    "className" : "PushUpdateRegistrationId" 
  }, 
  "host" : { 
    "process" : "5832@ERIKSWEDMINI", 
    "name" : "ERIKSWEDMINI", 
    "ip" : "192.168.1.150" 
  } 
}

这是我的POJO我试图映射到

@Entity(value = "log") 
public class log 
{ 
    @Id private ObjectId id; 
    Date timestamp; 
    Level level; 
    String thread;
    String message; 

    @Embedded  
    LoggerName loggerName; 

    String fileName; 
    String method; 
    String lineNumber; 

    @Embedded("class") 
    LoggerClass loggerClass; 

    @Embedded
    MyHost host; 
} 

@Entity(noClassnameStored=true) 
@Embedded 
class LoggerName 
{ 
    String fullyQualifiedClassName; 
    @Property("package") 
    String[] _package; 
    String className; 

} 

@Entity(noClassnameStored=true) 
@Embedded 
class LoggerClass 
{ 
    String fullyQualifiedClassName; 
    @Property("package") 
    String[] _package; 
    String className; 

} 

@Entity(noClassnameStored=true) 
@Embedded 
class MyHost 
{ 
    String process; 
    String name;
    String ip;
} 

1 个答案:

答案 0 :(得分:1)

morphia.map是实例方法。您需要morphia.map在Morphia的同一个实例上morphia.fromDBObject才能让{{1}}工作