Neo4j OGM Connect to existing Embeded DB - neo4j

good day. i have some theoretical question.
i made application that uses neo4j to traverse graphs and make some external calculations on each node. traverse process initiated by external event.
during traverse process nodes properties may change according calculation results.
this made by
new GraphDatabaseFactory().newEmbeddedDatabase(new File (this.DB_PATH));
GraphDatabaseService.traversalDescription();
now i'd like to use neo4j OGM to use domain classes in path extenders code instead self-made node-wrappers. also i`d like to use domain classes to populate DB with nodes and relations , and change properties of nodes from external source (REST for example).
But i can't find way to get Session or SessionFactory to existing and operational database.
is it possible to connect to database , created as embedded and conducting some calculations in one module , from another module by BOLT protocol, execute some queries, use OGM features?

The OGM cannot be used from path expanders / traversals (at least not easily).
To use OGM with embedded database you have 2 options
use the standard configuration as described in the reference documentation
set use Components.setDriver to set the driver manually, see e.g. this answer for details

Related

Dynamic Spring Data Classes for Neo4J based on an Ontology

I have to create a web service that needs to interact with a Neo4J database using the Spring framework with Spring-Data-Neo4J. This requires a static data domain model, e.g. defined labels, relations, properties.
The Problem is, that my data is based on an Ontology (via the neosemantics plugin) which could be modified in the future. It would be great if the application could automatically adopt to it. This way, the data model could be extended by editing the ontology only and no additional programming knowledge would be necessary.
Does this mean I have to generate the Spring data classes dynamically (based on the Ontology) or is there a better way to achieve this with Spring-Data-Neo4J (or should I use a different framework)?
Sure, you could come up with a way to generate a set of classes from an ontology. But that is probably going to present more problems than it solves.
An automatically-generated set of classes may not correspond to an appropriate data model for your use cases. The determination of the appropriate data model still requires a human.
Also, the new classes may be incompatible with the existing client code. And you may have to migrate the existing DB over to the new data model. Fixing all that requires humans as well.
I ended up using the Java neo4j driver instead of Spring-Data-Neo4jand a generic node class implementation only having the fields id, list of labels and a map of properties. The set labels and properties can be checked against the ontology prior to creating the nodes in the database. This way I can enforce a specific set of node labels and properties by only modifying the ontology and without having to generate the specific Spring-Data-Neo4j data classes.

Odata Open Type and Odata Client Library

I have a business requirement where I need to expose set of custom properties defined by user and since this is user configuration, I cannot go away by creating classes. Therefore I need to opt for open types feature in Odata.
Q1. Is there any sample implementation out there on how I can persist the data to database and also support the querying capabilities on open types?
Q2. One issue I noticed is currently client library is not correctly handling open types and can only be achieved by partial classes that means user has to know the custom properties up front so that they can hand craft partial classes which is not what I want to do. Instead better approach would have been to support open types on client side by dynamic properties. Any pointers on how the client side experience can be optimized.
About query capabilities on open types, order and filter is supported in v5.5(will be released by the end of this month), query the value of dynamic properties, you can follow this pull request, part of this is in master branch now.
About persist the data to database, I think you can consider non-relational database, which can be a good choice for your open type data.
About Q2, achieve by dynamic properties is not implement in client library, maybe you can open an issue in github for us.

Using Neo4j in Grails without the Grails neo4j-plugin and GORM

Is it possible to use Grails to provide Controllers and Views, Neo4j as the database and (self written) domain classes that wrap the database access and CRUD operations without the neo4j plugin?
The data I have (~10^6 Nodes, 10^7 Relationships) are very well suited to be modeled by a graph DB. The Nodes and the relationships both need to have labels and properties so they can be accessed through traversal methods that only go via certain paths in the graph. I want to use grails for the web interface because I just starting learning programming a few weeks ago and it appears to be a pretty good point to begin.
From what I understand until know is that with the Grails Neo4j-plugin, it is not possible to set relationships with properties and labels. It seems very appealing and easy to write the classes that relate to the data using the plain Neo4j-Java-API.
Additionally, if my database is already structured in a way that directly relates to Objects, what is the benefit of using ORM (or object-graph-mapping in this case)?
Unless you require Grails scaffolding and you're not depending on domain classes in Grails you can go without the GORM plugin and do the dirty work on your own.
Add the neo4j jar dependencies to your BuildConfig.groovy and expose the GraphDatabaseService and optionally the ExecutionEngine to your application context, see http://grails.org/doc/latest/guide/spring.html#springdslAdditional.
In the near future there will be 2.0 version of the Neo4j GORM plugin that uses labels and relies solely on Cypher. Relationship properties is high on the list after this release.

Neo4j project structure

I am looking to build a relatively complex Neo4J application, which I intend to split up in two separete projects, namely frontend and backend. The frontend will be HTML5 and is not relevant for this question, the backend will have a REST interface with Jersey, but it's the structure behind that REST interface that I have questions about.
Atm, this is how I envisioned it :
RESTimpl <-DATA-> Service <-DTO-> Repository <-NODE-> DAO <--> Neo4j Singleton
The general flow would be that the RESTimpl receives JSON and converts it to simple java objects like Strings, int, ... Those are then passed on to the service who creates a DTO with them. That DTO is passed on to the repository which performs all the DAO calls needed to write such a DTO to the database (one DTO may require several nodes and relations to be created). For DAO I was thinking of creating both a Core API and Cypher implementation, which has just very basic graph functions like creating a node, creating a relation, deleting a node, ... Methods which are useful for all repositories basically. The Neo4j singleton would then contain my GraphDatabaseService instance and some configuration stuff.
This is a relatively complex structure but I want the project to be very modular. Makes it easy to do Dependency Injection. (Everything will be written against an interface as well)
However, all the examples on the internet have a different implementation. They actually make their DTO's a wrapper around the Neo4J node or at least store the underlying node in the DTO. But it does allow for just a REST-Service-DAO structure.
But it doesn't allow me to change the repository implementations and put a different database behind the application.
What would be the "most correct way" to do what I want to do?
I use exactly what you've described above and I find that it works very well(one project in production, one almost there) and does not mix concerns. I don't use Spring Data but it is an option to consider.
I have defined my domain objects as standard POJO's- no Neo4j stuff in them at all. Their persistence is managed by DAO's- which contain mostly Cypher queries, and in some cases some core API work depending on complexity.
The GraphDatabase is a injected (I have two contexts- the EmbeddedGraph implementation is injected for production and the ImpermanentGraph for tests).
A REST service is exposed using Jersey which deals with domain objects and/or DTO's.
So Neo4j code is only seen in the persistence layer.
Got a couple of general convenience methods exposed to index/fetch by index etc.
I wouldn't go the wrap-Node way- tried it but found that it brings along its own set of problems and results in a somewhat smelly design. Looks like you're on the right track (to me at least)

Grails - a conditional mapping

Currently I'm developing a webapp which uses Oracle as a database. I've already been informed that my webapp has to work also on Sybase ASE. My Oracle database uses sequences to generate IDs and I mapped my domain classes to use those sequences. As far I know Sybase ASE doesn't have sequences but identities. And here is my problem. What to do with code/configuration to work with Oracle and Sybase. Some "conditional mapping" of ID for each domain would do.
I just don't want to comment/uncomment lines in mappings every time when I'm building webapp for other database than already mapped. Have anybody any idea what to do?
There is another danger: another differences between Oracle and Sybase which have influence on mapping (I don't generate database structure using dbCreate = "create") . At this point I don't see anything. Any experiences?
EDIT:
It turned out that it's not possible make one mapping for both databases. My Oracle structure uses quotes in column and table names. And quotes are not allowed in Sybase ASE. My (not nice and pretty) solution is changing (comment/uncomment) mapping when building webapp on some particular platform.
Btw I'm huge fan of Grails and this mapping thing seems to be a drawback. If I was using "pure" Hibernate I'd have two mappings in xml files and I'd change them depending on an underlying database. But Grails doesn't give me possibility to have two mappings.
I don't know if grails enable to you to configure coditional mapping for id generation but I think that maybe you don't need use neither sequences or identities, and use other hibernate id generator strategy: http://grails.org/doc/1.3.7/ref/Database%20Mapping/id.html.
EDIT:
Reviewing the hibernate documentation I found that is possible use sequences or identites according to capacities of database, from http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id:
All generators implement the interface
org.hibernate.id.IdentifierGenerator. This is a very simple interface.
Some applications can choose to provide their own specialized
implementations, however, Hibernate provides a range of built-in
implementations. The shortcut names for the built-in generators are as
follows:
...
native:
selects identity, sequence or hilo depending upon the capabilities of
the underlying database.
So you maybe will need a configuration like this:
static mapping = {
id generator:'native'
}

Resources