OrientDB for Play! Framework
- play.db.Model support
- CRUD support
- Embedded server for development
- Injection of database sessions in controllers
- Annotation-based transaction demarcation
- Live class-reloading as usual
- Basic support for RecordHooks and DatabaseListeners
These are the default values that you can override in conf/application.conf
# OrientDB Module
# ~~~~~
# odb.url=memory:temp
# odb.user=admin
# odb.password=admin
# Separate url for graphDB, if not specified uses odb.url
# odb.graph.url=memory:temp
# Path to custom configuration file, in classpath or retalive to conf/ dir
# odb.config.file=/play/modules/orientdb/db.config
# Package prefix to scan entities
# odb.entities.package=models
# Control open session in view
# odb.open-in-view.documentdb=true
# odb.open-in-view.objectdb=true
# odb.open-in-view.graphdb=false
Note: Please, set odb.open-in-view.[dbtype] to true only for the database types that you want to inject. By default all are enabled.
The access to ODatabaseObjectTx is wrapped in Model, so if you don’t need direct access simply create an entity that extends play.modules.orientdb.Model
public class Item extends Model { @SuppressWarnings("unused") @Id private Object id;
@SuppressWarnings("unused") @Version private Object version;
@Required public String name;
public String description; }
And use that entity on the controller:
public class MyController extends Controller {
public static void index() { OObjectIteratorClass<Item> items = Item.all(); render(items); }
public static void detail(ORecordId id) { Item item = Item.findById(id); notFoundIfNull(item); render(item); }
@Transactional public static void save(String name, String description) { Item item = new Item(); item.name = name; item.description = description; item.save(); index(); }
public static void search(String query) { List<Item> result = Item.find("select * from Item where name like ?", "%"+query+"%"); render(result); } }
Note: to obtain the ORID call the method entity.getIdentity()
Inject the session in the controller
public class MyController extends Controller {
@Inject static ODatabaseDocumentTx docdb;
...
@Transactional(db = DBTYPE.DOCUMENT) public static void good() { ODocument doc = new ODocument(docdb, "Account"); doc.field("name", "good"); doc.save(); index(); }
}
If you have an entity that maps the document you can obtain an object representation, because OrientDB ODatabaseObject is built on top of ODatabaseDocument
public class MyController extends Controller { @Inject static ODatabaseObjectTx db;
public static void index() { OObjectIteratorMultiCluster<Account> accounts = db.browseClass(Account.class); render(accounts); }
}
A simple test:
public class MyController extends Controller { @Inject static OGraphDatabase database;
public static void someMethod() { OClass vehicleClass = database.createVertexType("GraphVehicle"); database.createVertexType("GraphCar", vehicleClass); database.createVertexType("GraphMotocycle", "GraphVehicle");
ODocument carNode = (ODocument) database.createVertex("GraphCar").field("brand", "Hyundai") .field("model", "Coupe").field("year", 2003).save(); ODocument motoNode = (ODocument) database.createVertex("GraphMotocycle").field("brand", "Yamaha") .field("model", "X-City 250").field("year", 2009).save();
database.createEdge(carNode, motoNode).save(); ... } ... }
If you need direct access to any ODatabase use play.modules.orientdb.ODB
ODatabaseDocumentTx docdb = ODB.openDocumentDB();
and so on
To register RecordHooks or DatabaseListeners, implement the proper interface (ORecordHook or ODatabaseListener) and put the code under /app, the module will find and register these classes at startup.
//