Morphia在MongoDB中插入多个嵌入数据

时间:2015-09-14 08:39:13

标签: java mongodb-java morphia

想要使用Morphia在mongoDB中插入多个嵌入式。我想插入这样的数据:

{“_ id”:ObjectId(“55f67a57128699b6e5eab719”),“className”:“com.emecpoc.entity.User”,“smeId”:“SME1”,“creator”:“admin@souvik.emec.co。在“,”dateOfUsage“ :“2015-06-29”, “UsageDetail”:[{“timeOfUsage”:“11:52:49.0”,“usageType”:“Storage Consumed”,“length”:“81326”}, {“timeOfUsage”:“12:12:55.0”,“usageType”:“Storage Consumed”,“length”:“115593”}, {“timeOfUsage”:“12:12:55.0”,“usageType”:“已使用存储空间”,“长度”:“5383663”}, {“timeOfUsage”:“11:52:49.0”,“usageType”:“已消耗存储空间”,“长度”:“46310”}] }

豆是这样的:

 @Entity("User")
    public class User {
        @Id
        ObjectId id;
        String smeId,creator,dateOfUsage;
        Timestamp created;
    @Embedded("UsageDetail")
        //List<UsageDetail> usageDetail;
        UsageDetail usageDetail;
//getter and setter methods
}

这是嵌入式bean类:

 public class UsageDetail {
      private String timeOfUsage;
      private String usageType;
      private String length;
    //getter and setter methods
    }

但我得到的结果如下:

    { "_id" : ObjectId("55f67a57128699b6e5eab71c"), "className" : "com.emecpoc.entity.User", "smeId" : "SME1", "creator" : "admin@souvik.emec.co.in", "dateOfUsage"
    : "2015-06-29", "UsageDetail" : { "timeOfUsage" : "11:52:49.0", "usageType" : "Storage Consumed", "length" : "46310" } }

If I remove @Id from User bean then results 4 data entry like this:
{ "_id" : ObjectId("55f67a57128699b6e5eab719"), "className" : "com.emecpoc.entity.User", "smeId" : "SME1", "creator" : "admin@souvik.emec.co.in", "dateOfUsage"
: "2015-06-29", "UsageDetail" : { "timeOfUsage" : "11:52:49.0", "usageType" : "Storage Consumed", "length" : "81326" } }

{ "_id" : ObjectId("55f67a57128699b6e5eab71a"), "className" : "com.emecpoc.entity.User", "smeId" : "SME1", "creator" : "admin@souvik.emec.co.in", "dateOfUsage"
: "2015-06-29", "UsageDetail" : { "timeOfUsage" : "12:12:55.0", "usageType" : "Storage Consumed", "length" : "115593" } }

{ "_id" : ObjectId("55f67a57128699b6e5eab71b"), "className" : "com.emecpoc.entity.User", "smeId" : "SME1", "creator" : "admin@souvik.emec.co.in", "dateOfUsage"
: "2015-06-29", "UsageDetail" : { "timeOfUsage" : "12:12:55.0", "usageType" : "Storage Consumed", "length" : "5383663" } }

{ "_id" : ObjectId("55f67a57128699b6e5eab71c"), "className" : "com.emecpoc.entity.User", "smeId" : "SME1", "creator" : "admin@souvik.emec.co.in", "dateOfUsage"
: "2015-06-29", "UsageDetail" : { "timeOfUsage" : "11:52:49.0", "usageType" : "Storage Consumed", "length" : "46310" } }

因此,请指导我如何在嵌入式部件中插入多个数据。

0 个答案:

没有答案