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