Reputation: 15490
There is a Country
class:
public class Country {
public int id;
public String name;
public String locale;
}
I have to insert into neo4j.
What I am doing:
public void insert(Country country) {
engine.query("create (n:Country {name:'"+country.name+"',id:"+country.id+",locale:'"+country.locale+"'})",null);
}
But I want to put Object directly to neo4j with label.
I am using play framework 2.3.7 with java 1.8 and libraryDependencies are
"org.neo4j" % "neo4j-rest-graphdb" % "2.0.1",
"com.sun.jersey" % "jersey-server" % "1.7",
"com.sun.jersey" % "jersey-core" % "1.7",
"com.sun.jersey" % "jersey-json" % "1.7",
Upvotes: 1
Views: 511
Reputation: 103
You can use spring-data-neo4j for your requirement.Detailed steps are
1.Add dependencies to build.sbt
"org.springframework.data" % "spring-data-neo4j" % "3.2.2.RELEASE",
"org.springframework.data" % "spring-data-neo4j-rest" % "3.2.2.RELEASE",
"org.springframework.data" % "spring-data-neo4j-tx" % "3.2.2.RELEASE"
Global.java
public class Global extends GlobalSettings {
private ApplicationContext ctx;
@Override
public void onStart(Application app) {
ctx = new AnnotationConfigApplicationContext(Neo4jConfig.class);
}
@Override
public <A> A getControllerInstance(Class<A> clazz) {
return ctx.getBean(clazz);
}
@Override
public void onStop(Application app){
((AnnotationConfigApplicationContext)ctx).close();
}
}
Configuration class for Neo4j connectivity
@Configuration
@EnableTransactionManagement
public class Neo4jConfig extends Neo4jConfiguration {
public Neo4jConfig() {
setBasePackage("model");// My Entity package
}
@Bean
public SpringRestGraphDatabase graphDatabaseService() {
return new SpringRestGraphDatabase("http://localhost:7474/db/data");
}
@Override
@Bean(name = "transactionManager")
public PlatformTransactionManager neo4jTransactionManager() throws Exception {
return new JtaTransactionManagerFactoryBean(getGraphDatabaseService()).getObject();
}
}
Annotate your country class with NodeEntity
@NodeEntity
public class Country{
public int id;
public String name;
public String locale;
}
Use Neo4jTemplate for saving object to Graph DB
@Autowired
Neo4jTemplate template;
public Country save(Country country){
return template.save(country);
}
Upvotes: 2