youseeus
youseeus

Reputation: 357

mongojack: Unrecognized field at subclass

I use mongojack to map my POJOs for mongoDB.

It works fine with a Single POJO. But if I make a Subclass of this an insert fails:

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

These are the classes:

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+"]";
    }
}

and this the code where the exception occurs:

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);

Upvotes: 2

Views: 1761

Answers (0)

Related Questions