mongojack:子类中无法识别的字段

时间:2013-12-13 14:06:43

标签: java mongodb jackson pojo mongo-jackson-mapper

我使用mongojack来映射我的POJO以用于mongoDB。

使用单个POJO可以正常工作。 但如果我创建了一个子类,则插入失败:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "created" (class de.hbt.dps.data.PoJo), not marked as ignorable (2 known properties: , "_id", "url"])

这些是类:

public class PoJo {

    @Id
    protected String id;

    protected String url;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "PoJo [id=" + id + ", url=" + url + "]";
    }    
}

public class SubPoJo extends PoJo {

    private Date created;

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    @Override
    public String toString() {
        return "PoJo [id=" + id + ", url=" + url + ", created="+created+"]";
    }
}

这是发生异常的代码:

MongoClient = new MongoClient(DB_SERVER, PORT);
DB db = mongoClient.getDB(DATABASE_NAME);
DBCollection dbColl = db.getCollection(SUBSCRIPTION_TABLE_NAME);

JacksonDBCollection<PoJo, String> coll = JacksonDBCollection.wrap(dbColl, PoJo.class, String.class);

// this works:
SubPoJo pojo = new PoJo();
pojo.setUrl("test");
coll.insert(pojo);

// this doesnt work:
SubPoJo pojo1 = new SubPoJo();
pojo1.setUrl("test");
pojo1.setCreated(new Date());
coll.insert(pojo1);

0 个答案:

没有答案