Skip to content
João Sousa edited this page May 13, 2017 · 6 revisions

Simple Mongo Object Framework (SMOF) is an ORM built in Java for MongoDB. Smof relieves the burden of dealing with object serialization and deserialization, as well as managing such objects in a data store. Furthermore, in order to achieve optimal performance, Smof caches objects through the Guava cache and uses ByteBuddy to lazy load objects from the database. Unlike other MongoDB ORMs, Smof is able to deal with complex object hierarchy schemas.

To get started with Smof:

  • Check the Prerequisites section on how to install the dependencies of this project;
  • Check the Getting Started section for a quick tour around this project;

Smof works as simple as:

public static class Bottle extends AbstractElement {

		private static final String CAPACITY = "capacity";
		private static final String AMOUNT = "liquid_amount";
		private static final String LIQUID = "liquid";

		@SmofString(name = LIQUID)
		private String liquid;

		@SmofNumber(name = AMOUNT)
		private double amount;

		@SmofNumber(name = CAPACITY)
		private double capacity;

		public Bottle(String liquid, double capacity) {
			this(liquid, capacity, 0.0);
		}

		@SmofBuilder
		public Bottle(@SmofParam(name=LIQUID) String liquid,
				@SmofParam(name = CAPACITY) Double capacity,
				@SmofParam(name = AMOUNT) Double amount) {
			this.liquid = liquid;
			this.capacity = capacity;
			this.amount = amount;
		}

		public boolean isFull() {
			return capacity == amount;
		}

		public double fill(Double amount) {
			final double left = capacity-amount;
			if(left < amount) {
				this.amount = capacity;
				return amount-left;
			}
			this.amount += amount;
			return left-amount;
		}
}

In order to perform some write operations (insert, update), all we have to do is:

public static void main(String[] args) {
		//create the smof object with host, port and database name
		final Smof smof = Smof.create("localhost", 27017, "myDB");
		//create a new bottle
		final Bottle bottle = new Bottle("water", 1.0);
		//create a collection and map it to a type
		smof.createCollection("bottles", Bottle.class);
		//saves the bottle
		smof.insert(bottle);
		//fill the bottle
		bottle.fill(0.5);
		//update the object on the database
		smof.update(Bottle.class).fromElement(bottle);

		smof.close();
}

Note: This wiki is a work in progress

Table of Contents

Clone this wiki locally