在mongodb中使用ISODate和play morphia

时间:2014-09-02 18:51:54

标签: mongodb playframework morphia

我想将日期类型的值保存为mongodb作为ISODate或任何类似的日期类型与play-morphia。但是,当我尝试将以下类保存到mongodb时,它会在lastcheckin上抛出异常。当我将其类型更改为String时,它工作正常。是因为我没有在JAVA中使用正确的数据类型来映射到mongodb中的ISODate吗?

@Entity(value = "devices", noClassnameStored = true)
public class Device {
    public String ip;
    public String mac;
    public Date lastcheckin;
}

将当前日期分配给lastcheckin属性

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
        device.lastcheckin = sdf.parse(Calendar.getInstance().getTime().toString());
    }
catch (ParseException e){
        PlayLogger.instance().info(e.getMessage());
}

获得以下stacktrace

Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException
    at com.google.code.morphia.mapping.ValueMapper.fromDBObject(ValueMapper.java:27)
    at com.google.code.morphia.mapping.Mapper.readMappedField(Mapper.java:497)
    at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:480)
    ... 51 more
Caused by: java.lang.IllegalArgumentException
    at java.util.Date.parse(Date.java:615)
    at com.google.code.morphia.converters.DateConverter.decode(DateConverter.java:32)
    at com.google.code.morphia.converters.DefaultConverters.fromDBObject(DefaultConverters.java:129)
    at com.google.code.morphia.mapping.ValueMapper.fromDBObject(ValueMapper.java:25)
    ... 53 more

1 个答案:

答案 0 :(得分:0)

一般情况下这应该没问题,只要java.util.Date

PS:public属性应该private / protected加上getter和setter。