couchdb - Simple CRUD with POJOs with CouchDB - couchdb tutorial - futon couch - nosql
Simple CRUD with POJOs
- The below example explained for creating a simple POJO and doing standard CRUD operation on it.
Creating a simple POJO
we define a POJO as follows
- The annotation
@JsonInclude(JsonInclude.Include.NON_NULL)
tells jackson not to serialize null fields into JSON. So, for example, if the id property is null you wont have the id property in the resulting JSON at all. - Additionally, the
@JsonProperty("_id")
and@JsonProperty("_rev")
annotations are directives, informing the serializer/unserializer what JSON properties to map these values to. Documents in CouchDB must have both a _id and a _rev field, thus all POJOs which you intent to persist in CouchDB, must include a id and revision properties as above. You are free to name your properties differently in the POJO, as long as you don't change the annotations.
For example,
Read Also
Opening a connection to couchdb.Persisting new instances to CouchDB
- Now, creating a brand new document, in the database is done as follows, presuming you have a valid CouchDbInstance , and that you wish to persist the document in a database named person
- Now, in this scenario, CouchDB will automatically create a new ID and Revision for you. If you dont want this, you can use
Loading, updating and deleting documents
Presuming you have a CouchDBConnector instance ready, we can load instances of our POJO as follows
then we can manipulate it, and update it as follows
Read Also
Connecting to a database.- Notice, if the revision of the document in couch, doesn't match the revision of the document you are sending, the update will fail, and you need to load the latest version from the database and merge your instances accordingly.
- And finally, we should delete the instance, its as simple as