Home > Uncategorized > Example for using Neo4j with Grails

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:

  1. Create your sample application:
    grails create-app neo4jtest; cd neo4jtest
  2. Add the Neo4j plugin:
    grails install-plugin neo4j
  3. create some sample domain classes:
    grails create-domain-class Author
    grails create-domain-class Book
  4. create a controller for the domain class
    grails create-controller Author
    grails create-controller Book
  5. 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]
    }
  6. modify the controller to use dynamic scaffolding:
    class AuthorController {
        def scaffold = true
    }
    class BookController {
        def scaffold = true
    }
  7. start up the application:
    grails run-app
  8. use it, love it: go to http://localhost:8080/neo4jtest, add some authors and books.
  9. to explore the Neo4j node space created with your grails app, check out Neoclipse.

UPDATE: Use Grails 1.2.1

Categories: Uncategorized Tags: ,
  1. Peter Neubauer
    October 6th, 2009 at 10:41 | #1

    Great to see some code, thanks!

    /peter

  2. October 16th, 2009 at 19:26 | #2

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

  3. October 18th, 2009 at 18:52 | #3

    Hi Polprav, quoting is allowed of course.

  4. Ricardo Villalobos
    March 19th, 2010 at 21:49 | #4

    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

  5. March 19th, 2010 at 22:55 | #5

    @Ricardo Villalobos
    Hi Ricardo, I wll take a look at this the next days. Need to install STS before… stay tuned.

  6. Ricardo Villalobos
    March 19th, 2010 at 23:04 | #6

    @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

  7. March 21st, 2010 at 13:33 | #7

    @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.

  8. Ricardo Villalobos
    March 21st, 2010 at 19:27 | #8

    @Stefan Armbruster
    Hi Stefan. It works!

    Thanks a lot!

    Sincerely,
    /ricardo

  1. No trackbacks yet.