Extends the mongodb module with object-document mapping via Morphia.
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-morphia</artifactId>
<version>1.4.1</version>
</dependency>Before you start make sure to read the doc from mongodb module. This module extends mongodb module.
application.conf:
db = "mongodb://localhost/mydb"{
use(new Monphia());
get("/", req -> {
Datastore ds = require(Datastore.class);
// work with mydb datastore
});
}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
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
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
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!!