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
20 replies on “Example for using Neo4j with Grails”
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
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
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 ?
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
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
@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.
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
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/
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.
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
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.
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)
This blog post is outdated, please see https://github.com/sarmbruster/neo4jsample for an example.