Example for using Neo4j with Grails
In reply to my today’s annoncement of the Neo4j Grails plugin, @StigLau asked me to provide an example for using the Neo4j Grails plugin. So here we go:
- Create your sample application:
grails create-app neo4jtest; cd neo4jtest - Add the Neo4j plugin:
grails install-plugin neo4j
- create some sample domain classes:
grails create-domain-class Author grails create-domain-class Book
- create a controller for the domain class
grails create-controller Author grails create-controller Book
- modify the domain classes:
import grails.plugins.neo4j.Neo4jEntity @Neo4jEntity class Author { String name Date dob static hasMany = [ books: Book ] }
and
import grails.plugins.neo4j.Neo4jEntity @Neo4jEntity class Book { String title static belongsTo = [author:Author] }
- modify the controller to use dynamic scaffolding:
class AuthorController { def scaffold = true }
class BookController { def scaffold = true }
- start up the application:
grails run-app - use it, love it: go to http://localhost:8080/neo4jtest, add some authors and books.
- to explore the Neo4j node space created with your grails app, check out Neoclipse.
UPDATE: Use Grails 1.2.1
Great to see some code, thanks!
/peter
Hello from Russia!
Can I quote a post in your blog with the link to you?
Hi Polprav, quoting is allowed of course.
Hi Stefan, I’ve tried your example but I get a Java Error “The type Node is ambiguous” repeated 5 times in the class Neo4jPluggingSupport. It seems that others are able to use your example without problem so I suspect that my IDE may be the problem. I’m using springsource’s STS 2.3.1 (Eclipse 3.5, Grails 1.2.1, Groovy 1.7).
I wanted to ask your opinion on this before creating an issue at codehaus for GRECLIPSE.
Thanks!
/ricardo
@Ricardo Villalobos
Hi Ricardo, I wll take a look at this the next days. Need to install STS before… stay tuned.
@Stefan Armbruster
Thanks Stefan! BTW, I’ve created a thread at the springsource forums asking for help. The thread is entitled “Ambiguous type Java problem when using Neo4j graph database plugin” and it can be found at:
http://forum.springsource.org/showthread.php?t=86463
@Ricardo Villalobos
Hi Ricardo, I’ve update the domain class code to contain @Neo4jEntity – thats necessary since the 0.2 version of the plugin. Please try this and see if the controllers are working as expected.
@Stefan Armbruster
Hi Stefan. It works!
Thanks a lot!
Sincerely,
/ricardo