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

20 thoughts on “Example for using Neo4j with Grails

  1. Ricardo Villalobos

    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

  2. preet

    Hi Stefan, i was trying to use neo4j plugin 0.3.1 with grails 1.3.7 and 1.1 neo4j db. Getting error at entity annotation, please help me.

    error
    ==
    Groovy:class grails.plugins.neo4j.Neo4jEntity is not an annotation in @grails.plugins.neo4j.Neo4jEntity

  3. Stefan Armbruster Post author

    If using from an IDE, make sure neo4jplugin’s src/ast directory is a source folder – that’s where Neo4jEntity annotation is located. If running grails run-app, do you see a line like ‘Precompiling neo4j plugin – required because of AST transformations…’ on stdout/stderr ?

  4. preet

    Thanks Stefan.

    I am using Eclipse IDE, i couldn’t find src/ast directory when i expanded plugin in eclipse.

    However when i do run-app i see fallowing, it says “Precompiling neo4j plugin – required because of AST transformations” and app running..

    how do i suppress eclipse error?

    ==
    Welcome to Grails 1.3.7 – http://grails.org/
    Licensed under Apache Standard License 2.0
    Grails home is set to: C:\grails_project\springsource-tool-suite-2.6.1.SR1-e3.6.2-win32-x86_64\springsource\grails-1.3.7\

    Base Directory: C:\grails_project\neotest
    Resolving dependencies…
    Dependencies resolved in 2560ms.
    Running script C:\grails_project\springsource-tool-suite-2.6.1.SR1-e3.6.2-win32-x86_64\springsource\grails-1.3.7\scripts\RunApp.groovy
    Environment set to development
    [echo] plugin files: file [C:\Users\preet\.grails\1.3.7\projects\neotest\plugins\neo4j-0.3.1-SNAPSHOT\.]
    [echo] Precompiling neo4j plugin – required because of AST transformations…
    [echo] src C:/Users/preet/.grails/1.3.7/projects/neotest/plugins/neo4j-0.3.1-SNAPSHOT C:\Users\preet\.grails\1.3.7\projects\neotest\plugin-classes
    [echo] done precompiling AST transformations
    [exec] The command attribute is deprecated.
    [exec] Please use the executable attribute and nested arg elements.
    [exec] FIND: Parameter format not correct
    [exec] Result: 2
    [groovyc] Compiling 1 source file to C:\grails_project\neotest\target\classes
    [delete] Deleting directory C:\Users\preet\.grails\1.3.7\projects\neotest\tomcat
    Running Grails application..
    Server running. Browse to http://localhost:8080/neotest

  5. preet

    Thanks Stefan.

    I am using SpringSource Tool Suite Version: 2.7.1.RELEASE, i couldn’t find src/ast under under plugin when i expand it.

    However i see “done precompiling AST transformations” when i do run-app

    How do i suppress IDE error at @Neo4jEntity?

    Thankyou

  6. Jean-Sébastien Stoffen

    Hi,

    Thank you for this plugin.
    I’ve one question : when I follow the example showed above, everything is working but the data are not saved in the graph : the org.codehaus.groovy.grails.plugins.neo4j.Neo4jController always show me an empty graph.

    Should I modify the DataSource.groovy?
    The example don’t speak about it.

    Regards,
    Jean-Sébastien Stoffen

  7. Jean-Sébastien Stoffen

    Hi,
    Thank you for your answer.
    I’m interested in the new version of your plugin.
    I’m discovering Grails Neo4j and Groovy.

    I’m still wondering about something : when I create a new instance of an object using a Grails controller, I save it and the graph is not updated. It’s probably something to set in the DataSource.groovy.
    Could you show me how to configure it?

    Thank you,
    Jean-Sébastien.

  8. Mark

    For some reason, I decided to use the latest of Neo4j and Grails which aren’t RELEASES but Milestones. NEO4j 1.5.M02 and Grails 2.0.0.RC1 and Spring-data-neo4j 2.0.0.M1. Do you think it is possible to use the plugin to set the basics and able to have Grails generate the scaffolding and Controllers. Then just add the newer features?

    Thanks

    Mark

  9. Karthik

    I am getting below error with grails 2.3.5

    | Error Compilation error: startup failed:
    F:\git_projects\neo4jtest\grails-app\domain\neo4jtest\Author.groovy: 3: unable to resolve class grails.plugins.neo4j.Neo4jEntity
    @ line 3, column 1.
    import grails.plugins.neo4j.Neo4jEntity
    ^

    F:\git_projects\neo4jtest\grails-app\domain\neo4jtest\Book.groovy: 3: unable to resolve class grails.plugins.neo4j.Neo4jEntity
    @ line 3, column 1.
    import grails.plugins.neo4j.Neo4jEntity
    ^

    2 errors
    | Error Error running script run-app: org.codehaus.groovy.grails.cli.ScriptExitException (Use –stacktrace to see the full trace)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>