Environment Setup of Spring Data in Grails Project

Posted By : Prabjot Singh | 30-Dec-2014

Here, I will discuss how to setup of spring data neo4j in grails project. So, before start to setup, we need to understands some basic things about spring data neo4j, which I will use in project.

 

Spring Data Neo4j Annotations

  • @NodeEntity = it is used to define our domain class

  • @GraphId = it is used to define node or relationship id

  • @GraphProperty = it is used to define properties of node or relationship class. It is optional,so we can omit this.

 

##Configuration##

1) Add dependency injection for spring data neo4j in BuildConfig.groovy

compile "org.springframework.data:spring-data-neo4j:3.2.0.RELEASE"

 

2) we have to do spring data neo4j configuration in resources.groovy. The configuration -)

beans = {
xmlns neo4j:"http://www.springframework.org/schema/data/neo4j"
xmlns context:"http://www.springframework.org/schema/context"
context.'component-scan'('base-package': 'neo4j')
neo4j.'repositories'('base-package':'neo4jRepository')
neo4j.'config'('storeDirectory':'target/db5', 'base-package':'neo4j')
}
</beans>

 

3) There are some configuration elements,you have to focus

a) neo4j:repositories-> it should be path of your repositories

b) storeDirectory -> it should be your database path

c) component-> it should be a path of your entity classes

 

4) Create a domain class using spring data neo4j annotation(@NodeEntity)

@NodeEntity
class Person {
@GraphId Long graphId;
String name;
}

 

5) Create a repository. according to spring data neo4j,name of repository should be like as name of your domain class and should be extend graphRepository

public interface PersonRepository extends GraphRepository<Person>{
}

 

6) To save the person entity, use repository's save method

def personRepository
def savePerson(){
Person person=new Person()
person.name="John"
personRepository.save(person)
}

 

 

 

About Author

Author Image
Prabjot Singh

Prabjot is a Java and Grails developer.He has a good hands on neo4J, AngularJS. He likes to work on new technologies

Request for Proposal

Name is required

Comment is required

Sending message..