Libraries
Articles

Ektorp

From Libzter

Share/Save/Bookmark
Jump to: navigation, search


ektorp

Developer(s)Henrik Lundgren (and friends)
Initial Release?
Current Version1.1.1
Language(s)Java
Licence(s)Apache License 2.0
Platform(s)Java

Contents

[edit] Overview

Ektorp is a Java library that provides persistency functions similar to JPA using [[1]] as storage engine. Since couchDB is schemaless and document based, it allows for simplicity and flexibility of this persistens API. Under the hood it uses Jackson for object serialization. All communication is done by JSON over HTTP. As with couchDB, it uses javascript map reduce views for database queries.


[edit] Features

  • Out of the box CRUD - Create Read Update Delete
  • Spring support
  • Automatic view (query) generation
  • Annotations
  • A serialized object with multiple owned objects can be stored as separate database documents for improved performance.

[edit] Example

Here is a simple CRUD example application with persisted objects using ektorp and CouchDB.

// A class that can be stored using ektorp. Revision and Id are needed values, 
// however, there is a base class to derive from to avoid annotations like this.
@JsonWriteNullProperties(false)
@JsonIgnoreProperties({"id", "revision"})
public class MyClass {
 
        @JsonProperty("_id")
        private String id;
 
        @JsonProperty("_rev")
        private String revision;
 
        private String value;
 
        private String type;
 
        public MyClass(String id){
           setId(id);
           setType("MyClass");
        }
 
        public MyClass(){ setType("MyClass");}
 
        public String setId(String s) {
                id = s;
        }
 
        public String getId() {
                return id;
        }
 
        public String getRevision() {
                return rev;
        }
 
        public void setValue(String s) {
                value = s;
        }
 
        public String getValue() {
                return value;
        }
 
        public void setType(String s) {
                type = s;
        }
 
        public String getType() {
                return type;
        }
}
 
// The repository class, the view "all" defines a Map view that retrieves all MyClass objects from 
//CouchDB. The property "type" or some other type identifier has to be added manually since couchDB
//is schemaless, it cannot otherwise filter out all objects of a certain type.
@View( name = "all", map = "function(doc) { if(doc.type == 'MyClass' ) emit( null, doc._id )}")
public class MyRepository extends CouchDbRepositorySupport<MyClass> {
 
                public MyClassRepository(CouchDbConnector db) {
                        super(MyClass.class, db);
                        initStandardDesignDocument();
                }
 
}
 
// test app. 
public class Test {
  public static void main(String arg[]){
        HttpClient httpClient = new StdHttpClient.Builder()
                                    .host("localhost")
                                    .port(5984)
                                    .build();
 
        CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
        CouchDbConnector db = new StdCouchDbConnector("testdatabase", dbInstance);
 
        MyRepository repo = new MyRepository(db);
        MyClass s = new MyClass("myObject");                
        repo.add(s);
        MyClass s2 = repo.get("myObject");
        repo.update(s2);
        repo.remove(s2);
        List<MyClass> repo.getAll();
   }
 
}

[edit] Resources

http://code.google.com/p/ektorp/ - Website with download, documentations, source code etc.

http://ektorp.org/ - Main website with links


[edit] See Also

  • Jackson A library manage by codehaus for processing JSON using java.
  • Json-simple A library for processing JSON by fangyidong.
  • JSON Java script library for doing JSON by JSON.org

Library Navigation

  • Language

  • Category
Google AdSense