morphia

morphia

Extends the mongodb module with object-document mapping via Morphia.

exports

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-morphia</artifactId>
  <version>1.5.1</version>
</dependency>

getting started

Before you start make sure to read the doc from mongodb module. This module extends mongodb module.

usage

application.conf:

db = "mongodb://localhost/mydb"
{
  use(new Monphia());

  get("/", req -> {
    Datastore ds = require(Datastore.class);
    // work with mydb datastore
  });
}

options

morphia callback

The Morphia callback let you map classes and/or set mapper options.

{
  use(new Monphia()
    .doWith((morphia, config) -> {
      // work with morphia
      morphia.map(MyObject.class);
    })
  );
}

For more detailed information, check here

datastore callback

The Datastore callback is executed only once, it’s perfect for checking indexes:

{
  use(new Monphia()
    .doWith(datastore -> {
      // work with datastore
      datastore.ensureIndexes();
      datastore.ensureCap();
    })
  );
}

For more detailed information, check here

auto-incremental ID

This modules comes with auto-incremental ID generation, usage:

{
  use(new Monphia().with(IdGen.GLOBAL); // or IdGen.LOCAL
}

ID must be of type: Long and annotated with GeneratedValue:

@Entity
public class MyEntity {
  @Id @GeneratedValue Long id;
}

There two ID gen:

  • GLOBAL: generates a global and unique ID regardless of entity type
  • LOCAL: generates an unique ID per entity type

entity listeners

Guice will create and inject entity listeners (when need it).

public class MyListener {

  private Service service;

  @Inject
  public MyListener(Service service) {
    this.service = service;
  }

  @PreLoad void preLoad(MyObject object) {
    service.doSomething(object);
  }

}

NOTE: ONLY Constructor injection is supported.

That’s all folks! Enjoy it!!