Skip to content

Milstein/morphium

 
 

Repository files navigation

morphium

Morphium - Java Object Mapper and Caching Layer for MongoDB

Morphium is a POJO Object mapper for Accessing Mongodb. Some of the main Features of Morphium:

  • actively developed
  • used by a number of projects (including holidayinsider.com)
  • Transparent access to mongo db
  • Transparent declarative (Annotation based) caching
  • Annotation based definition of mappings
  • Cluster Awareness
  • Cache synchronization between cluster nodes
  • Asynchronous and Buffered write access
  • Messaging
  • fluent interface for querying mongodb
  • support for the mongodb aggregator framework
  • support for complex queries
  • support inheritance and polymorphism
  • support for javax.validation annotations
  • lifecycle method of pojos
  • nearly every aspect of morphium can be replaced by own implementation (e.g. Query-Object, CacheImplementation...)
  • ConfigManager helps storing app configurations in Mongo with efficient access to it (cached)
  • Support for References, including lazy loaded references
  • Support for partial updated objects (when writing, only the changes of the object are transferred)

for questions and feature requests / bug reports also have a look at the google group morphium-discuss@googlegroups.com

Quick Start

before accessing mongo via morphium, you need to configure morphium. this is done by preparing a MorphiumConfig Object:

  MorphiumConfig cfg = new MorphiumConfig();
  cfg.setDatabase("testdb");
  cfg.addHost("localhost", 27017);

you can also configure morphium using properties: new MorphiumConfig(properties); or a json-String: MorphiumConfig cfg = MorphiumConfig.createFromJson(json);

After that, you just need to instantiate morphium:

  Morphium m=new Morphium(cfg);

There are some convenience constructors available since V2.2.23 which make your life a bit easier:

   Morphium m=new Morphium("localhost","test-db");

this creates a morphium instance with most default settings, connecting to localhost, standard port 27017 and using database test-db

if necessary, it's of course possible to specify the port to connect to:

  Morphium m=new Morphium("localhost:27019","test-db");
  Morphium n=new Morphium("localhost",27020,"test-db");

then you are good to go:

  Query<MyEntity> q=m.createQueryFor(MyEntity.class).f("a_field").eq("a value");
  List<MyEntity> lst=q.asList();
  MyEntity ent=q.get();
  ...
  m.store(ent);

Defining an Entity is quite simple as well:

  @Entity(translateCamelCase = true)
  @Cache
  public class MyEntity {
    @Id
    private ObjectId myId; 
  
    private String aField;
  
    private EmbeddedObject emb;
  
    @Reference
    private MyEntity otherEntity;
  
   ...
  }

  @Embedded
  public class EmbeddedObject {
   ...
  }

All entities need to have an @Id field. If the type is org.bson.types.ObjectId, it will be created by mongo, if not - you need to take care of that. References only work to other entities (of course). You can also use Maps, Lists or Arrays, all may also include other Entities or Embedded types.

Use enum instead of strings for queries

As using strings to query your object might be a bit error prone, you also can use enums instead of field name strings:

   Query<MyEntity> q=m.createQueryFor(MyEntity.class).f(MyEntity.Fields.aField).eq("a value");

of course, these enums need to be created. have a look at https://github.com/sboesebeck/intelliJGenPropertyEnumsPlugin for a plugin for generating those automatically in our example, the result would look like this:

  @Entity(translateCamelCase = true)
  @Cache
  public class MyEntity {
    @Id
    private ObjectId myId; 
  
    private String aField;
  
    private EmbeddedObject emb;
  
    @Reference
    private MyEntity otherEntity;
    ...
    public enum Fields { myId, aField, emb, otherEntity }
   
  }

  @Embedded
  public class EmbeddedObject {
   ...
  }

This is a very short glance at all the features of Morphium!

For more information take a closer look at the wiki.

Have fun,

Stephan

About

Morphium - Java Object Mapper and Caching Layer for MongoDB

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 98.9%
  • Other 1.1%