IAmYourFaja
IAmYourFaja

Reputation: 56884

Neo4j and ORM (Hibernate)

I've been using RDBMSes since college and am really struggling with the underlying concepts of NoSQL databases...but I think their concept is really cool.

I believe I understand the following (please correct me if I'm wrong, because these play into my question here!):

If these are true, then let us redirect our attention to Neo4j, a "graph-based" database.

After perusing the site and the PDF, it seems like Neo4j is not only a database, but it also provides a Java API that essentially replaces the need for traditional ORM tools like Hibernate.

So, my final question is actually a request for clarification/confirmation of that last assertion, specifically:

Thanks in advance!

Upvotes: 16

Views: 11989

Answers (6)

Have you considered using Ferma? It is an ORM that is specific to Graph Databases. It supports all the major brands including Neo4J. Here is a description from the project.

Ferma is a robust framework that takes on a role similar to an Object-relational Model (ORM) library for traditional databases. Ferma is often referred to as a Object-graph Model (OGM) library, and maps Java objects to elements in a graph such as a Vertex or an Edge. In short it allows a schema to be defined using java interfaces and classes which provides a level of abstraction for interacting with the underlying graph.

Upvotes: 0

Aram Kocharyan
Aram Kocharyan

Reputation: 20421

As of version 4 beta, Hibernate OGM has added support for Neo4j:

Hibernate OGM can now work with Neo4j, a fully transactional property graph database.

Graph databases represent the data as a system of interconnected nodes and it works very well when you need to store information like the relationship between people in social networks for example. In addition, in a property graph, you can also add properties to the elements of the graph.

Hibernate OGM maps an entity as a node where the attributes of the entity become properties of the node. At the moment we add some additional properties for the conversion of the node into the entity but we are planning to replace them using the label mechanism added in the latest Neo4j versions.

The relationship between two nodes represents an association between two entities. Currently a bidirectional association requires two relationships but we are going to change this since Neo4j can navigate a relationship in both directions.

The integration with Neo4j is experimental but we are planning on improving it soon. Please, let us know what you think or help us improving it.

http://planet.jboss.org/post/hibernate_ogm_4_0_0_beta4_is_out

It's still very early days and the initial support is likely to improve over time. I'm in the process of trying to set it up and try it out. I'm aware that there's also a Spring project which has better integration.

Upvotes: 3

Bobby Norton
Bobby Norton

Reputation: 1504

I've been using RDBMSes since college and am really struggling with the underlying concepts of NoSQL databases...but I think their concept is really cool.

A graph database like Neo4j expresses a domain in terms of vertices connected to other vertices with edges. An edge contains its start and end vertex. Each vertex and edge can have a map of properties, key-value pairs that can be used to store additional information about the vertices and edges. You can, of course, extend this with your own domain, but things are simple to start out.

To see these concepts in action, I recommend the Getting Started Guide for Gremlin. Gremlin is a domain-specific language for traversing graphs that works with Neo4j and several other graph databases. Gremlin is to a graph database what SQL is to a relational database.

I can't recommend Gremlin strongly enough while you're learning about graphs. In just a few minutes, you can be up and running working through the Gremlin tutorials. Gremlin will provide you with a REPL that will let you experiment with small graphs and get immediate feedback. Even if you don't use Gremlin in your production system, the knowledge gained at the REPL will help you validate your designs and can serve as a precursor to more rigorous unit testing and development.

If you prefer working directly with Neo4j's API's, their traversal framework tutorial should help.

Is it true that if my backend is entirely Neo4j-based, that I would have no need for Hibernate (which is my usual ORM)?

Since you're new to Neo4j, I would recommend that you avoid ORM's until you first understand what the ORM needs to do for you. See how much pain you're really going to experience mapping the results of your query to your domain. If the pain can be ameliorated by an ORM, the Spring-Data framework mentioned by Peter may be useful.

In all likelihood, you will probably be just fine. I have worked on several projects where the accidental complexity introduced by the ORM far outweighed the benefits. Mapping the query results to the domain was in no way the most complicated part of the system.

Upvotes: 9

Peter Neubauer
Peter Neubauer

Reputation: 6331

Instead of Hibernate, I would take a look at http://www.springsource.org/spring-data/neo4j which is annotation driven, supported by Spring and works very well. Would this be something to work with?

Upvotes: 7

Daniel Camarda
Daniel Camarda

Reputation: 1126

As @assylias said you would not be able to use Hibernate as an ORM for a Graph DB like Neo4J, but there are other solutions.

In first place you can use the Neo4J api to traverse the graph and retrieve Vertices and Edges, consider that it is not an ORM so it would not map the vertices and/or edges you retrieve to your custom entities like Hibernate does.

This can be a solution but you would end up with code that is written specifically for Neo4J which have his own api, different from others graph databases (like OrientDB for example) and going on with the development you would probably need a more flexible way of retrieving data and map the results to your objects, so i suggest to take a look at the Tinkerpop stack (http://tinkerpop.com/) that is basically a series of java apis and abstraction layers to work with graph databases.

Start looking at Blueprint (http://blueprints.tinkerpop.com) that is an abstraction layer over the main concepts of a graph DB, so you can write code that does not depends on a specific DB vendor, then take a look at Frames (http://frames.tinkerpop.com/) an ORM like framework to map objects to vertices and edges, and Gremlin (http://gremlin.tinkerpop.com/) a language to easily query the graph.

Upvotes: 2

assylias
assylias

Reputation: 328568

AFAIK, Hibernate is an object/relational mapping framework that only supports SQL-like databases. So you won't need / be able to use it if you use Neo4j, you would use Neo4j's API instead.

But nothing prevents you from using both Neo4j and an SQL database therefore mixing Hibernate and the neo4j API (most likely to store/query different objects within your project).

Have you checked the basic examples given on Neo4j website, such as http://docs.neo4j.org/chunked/snapshot/tutorials-java-embedded-hello-world.html ?

EDIT:

You are right, NoSql does not define a specific standard. You might want to have a look at this (short) introduction: http://martinfowler.com/articles/nosql-intro.pdf

Upvotes: 5

Related Questions