2

我使用mongojack为 mongoDB 映射我的 POJO。

它适用于单个 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);
4

0 回答 0