public class Monphia extends Mongodb
Mongodb with object-document mapping via Morphia.
Exposes Morphia and Datastore services.
application.conf:
db = "mongodb://localhost/mydb"
{
use(new Monphia());
get("/", req -> {
Datastore ds = req.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
This 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:
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.
| Constructor and Description |
|---|
Monphia()
Creates a new
Monphia using the default property: db. |
Monphia(String db)
Creates a new
Monphia module. |
| Modifier and Type | Method and Description |
|---|---|
void |
configure(org.jooby.Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder) |
Monphia |
doWith(BiConsumer<org.mongodb.morphia.Morphia,com.typesafe.config.Config> callback)
Morphia startup callback, from here you can map classes, set mapper options, etc..
|
Monphia |
doWith(Consumer<org.mongodb.morphia.Datastore> callback)
Datastore startup callback, from here you can call Datastore.ensureIndexes(). |
Monphia |
with(IdGen gen)
Setup up an
EntityInterceptor on PrePersist events that generates an
incremental ID. |
public Monphia(String db)
Monphia module.db - Name of the property with the connection URI.public Monphia()
Monphia using the default property: db.public Monphia doWith(BiConsumer<org.mongodb.morphia.Morphia,com.typesafe.config.Config> callback)
callback - Morphia callback.public Monphia doWith(Consumer<org.mongodb.morphia.Datastore> callback)
Datastore startup callback, from here you can call Datastore.ensureIndexes().callback - Datastore callback.public Monphia with(IdGen gen)
Setup up an EntityInterceptor on PrePersist events that generates an
incremental ID.
{
use(new Monphia().with(IdGen.GLOBAL);
}
ID must be of type: Long and annotated with GeneratedValue:
@Entity
public class MyEntity {
@Id @GeneratedValue Long id;
}
gen - an IdGen strategyCopyright © 2016. All rights reserved.