ektorp / CouchDB混合使用HashMap和Annotations

时间:2012-05-20 08:59:08

标签: couchdb ektorp

在jcouchdb中我曾经扩展BaseDocument,然后以透明的方式混合Annotations而不是声明的字段。 例如:

import org.jcouchdb.document.BaseDocument;

public class SiteDocument extends BaseDocument {
    private String site;

    @org.svenson.JSONProperty(value = "site", ignoreIfNull = true)
    public String getSite() {
        return site;
    }

    public void setSite(String name) {
        site = name;
    }
}

然后使用它:

// Create a SiteDocument
SiteDocument site2 = new SiteDocument();
site2.setProperty("site", "http://www.starckoverflow.com/index.html");
// Set value using setSite
site2.setSite("www.stackoverflow.com");
// and using setProperty
site2.setProperty("description", "Questions & Answers");
db.createOrUpdateDocument(site2);

如果我同时使用通过注释定义的文档字段(站点)和未定义的属性字段(描述),则在保存文档时都会序列化。

这对我来说很方便,因为我可以使用半结构化文档。

当我尝试对Ektorp做同样的事情时我使用注释和使用HashMap的文档但我找不到一种简单的方法来获得两者的混合(我尝试使用自己的序列化器但是这似乎很多为我在jcouchdb中免费获得的东西工作。还试图注释一个HashMap字段,但后来被序列化为一个对象,我得到的字段自动保存在一个名为HashMap字段的对象中。

使用Ektorp是否可以(轻松/免费)?

1 个答案:

答案 0 :(得分:0)

绝对有可能。您有两种选择:

  1. 将您的课程基于org.ektorp.support.OpenCouchDbDocument
  2. 使用@JsonAnySetter和@JsonAnyGetter为您的类添加注释。红色更多:http://wiki.fasterxml.com/JacksonFeatureAnyGetter
相关问题