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

  9. preet
    July 18th, 2011 at 23:56 | #9

    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

  10. July 19th, 2011 at 09:22 | #10

    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 ?

  11. preet
    July 19th, 2011 at 16:41 | #11

    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

  12. preet
    July 19th, 2011 at 17:49 | #12

    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

  13. July 19th, 2011 at 19:34 | #13

    @preet
    Did not use STS recently. In IntelliJ I’ll open the project structure, navigate to the neo4j plugin and mark src/ast as source folder.

  14. Jean-Sébastien Stoffen
    August 6th, 2011 at 17:40 | #14

    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

  15. August 6th, 2011 at 18:10 | #15

    The plugin uses grails.neo4j.storeDir from Config.groovy to specify the directory where Neo4j writes to. Be aware, I’m currently in the stage of preparing a new release of the neo4j plugin providing full GORM compliance – unfortunately this will not be 100% compatible to the <0.3.1 versions. For the new stuff, look at https://github.com/SpringSource/grails-data-mapping/

  16. Jean-Sébastien Stoffen
    August 7th, 2011 at 15:12 | #16

    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.

  17. Mark
    November 9th, 2011 at 05:50 | #17

    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

  18. November 29th, 2011 at 17:18 | #18

    The new (and still not officially published) plugin is located at https://github.com/SpringSource/grails-data-mapping. I’m gonna talk about that next week at grailsxchange in London, shortly after the talk I plan to do a detailed blog post about this. NB the new plugin does not use spring-data-neo4j in any way. Using scaffolding is possible with the new plugin.

  19. Karthik
    February 26th, 2014 at 12:59 | #19

    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)

  20. admin
    February 26th, 2014 at 13:13 | #20

    This blog post is outdated, please see https://github.com/sarmbruster/neo4jsample for an example.

  1. No trackbacks yet.