pkg persistence DbService - yshehab/SchoolRoomBooking GitHub Wiki
DbService Class
Description
A class to provide the main services of the graph database by loading the configurations and exposing access to the Beans provided by spring-data-neo4j's factory.
It wraps the configuration class to consolidate the service in one class.
Package
Constructor
None defined in the class.
Static Members
private static final ApplicationContext ctx
Provides Bean-factory methods for accessing application components.
Attributes
None
Links
None
Protocol
This is a service class, hence all protocol is static.
Static Methods
protected static Neo4jTemplate getNeo4jTemplate()
Post condition: return a mediator object to access Neo4j services.
protected static GraphDatabaseService getGraphDb()
Post condition: return the singleton graphDb instance - exposing access lower than OGM.
protected static <T> T getRepository(Class<T> clazz)
Post condition: return A repository implementation for T -an instance. Loading a bean for a repository implementation in a generic fashion.
Inner Classes
DbConfig
A class for setting up spring-data-neo4j configurations for OGM.
Members
private static final String storeDirectory
Location of the embedded data store.
Protocol
GraphDatabaseService graphDatabaseService()
Provides an implementation of GraphDatabaseService as a Bean definition - required by Neo4jConfiguration.
Examples
Using a repository
private static final BookingRepository bookingRepo =
DbService.getRepository(BookingRepository.class);
public static Booking perisitBooking(Booking aBooking)
{
return bookingRepo.save(aBooking);
}
Using Neo4jTemplate
private static final Neo4jTemplate neo4jMediator = DbService.getNeo4jTemplate();
public static void persistEntity(EntityToSave anEntity)
{
Transaction tx = DbService.getGraphDb().beginTx();
try {
neo4jMediator.save(anEntity);
tx.success();
} finally {
tx.finish();
}
}
Using lower level than OGM
public static void printAllNodes()
{
String out;
for (Node n : GlobalGraphOperations.at(DbService.getGraphDb()).getAllNodes()) {
if (n.hasProperty("__type__")) {
out = "" + n.getId();
for(String key: n.getPropertyKeys()) {
out += "\t" + key + ": " + n.getProperty(key);
}
System.out.println(out);
}
}
}