mongojack: Unrecognized field at subclass

1,007 views
Skip to first unread message

yous...@gmail.com

unread,
Dec 16, 2013, 3:16:22 AM12/16/13
to mongo-jack...@googlegroups.com
I use mongojack to map my POJOs for mongoDB.

It works fine with a Single POJO.
But if I make a Subclass of this, the 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"])



What's wrong?

This are the classe:

   
    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 doesn't work:
   
SubPoJo pojo1 = new SubPoJo();
    pojo1
.setUrl("test");
    pojo1
.setCreated(new Date());
    coll
.insert(pojo1);

mkr...@trialfire.com

unread,
Dec 18, 2013, 1:06:35 PM12/18/13
to mongo-jack...@googlegroups.com
add the following annotation to your subclass : @JsonIgnoreProperties(ignoreUnknown = true)

yous...@gmail.com

unread,
Dec 19, 2013, 4:49:21 AM12/19/13
to mongo-jack...@googlegroups.com
So mongojack can't discover subclasses automatic?
I thought, pojo1 would be inserted as SubPoJo not as PoJo?

Best wishes
Josias

btw: this line:

    SubPoJo pojo = new PoJo();
should be replaced by
    PoJo pojo = new PoJo();
copy/past ;-)
Reply all
Reply to author
Forward
0 new messages