Java 使用 restapifacade 在 play 框架中的 Neo4j 上进行对象映射
Java Object Mapping on Neo4j in play framework using restapifacade
有个Country
class:
public class Country {
public int id;
public String name;
public String locale;
}
我必须插入到 neo4j 中。
我在做什么:
public void insert(Country country) {
engine.query("create (n:Country {name:'"+country.name+"',id:"+country.id+",locale:'"+country.locale+"'})",null);
}
但是我想把Object直接放到neo4j中带label。
我正在使用带有 java 1.8 和 libraryDependencies 的 play framework 2.3.7
"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",
您可以使用 spring-data-neo4j 因为您的 requirement.Detailed 步骤是
1.Add 依赖于 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();
}
}
Neo4j 连接的配置class
@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();
}
}
用 NodeEntity
注释你的国家 class
@NodeEntity
public class Country{
public int id;
public String name;
public String locale;
}
使用 Neo4jTemplate 将对象保存到 Graph DB
@Autowired
Neo4jTemplate template;
public Country save(Country country){
return template.save(country);
}
有个Country
class:
public class Country {
public int id;
public String name;
public String locale;
}
我必须插入到 neo4j 中。
我在做什么:
public void insert(Country country) {
engine.query("create (n:Country {name:'"+country.name+"',id:"+country.id+",locale:'"+country.locale+"'})",null);
}
但是我想把Object直接放到neo4j中带label。
我正在使用带有 java 1.8 和 libraryDependencies 的 play framework 2.3.7
"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",
您可以使用 spring-data-neo4j 因为您的 requirement.Detailed 步骤是
1.Add 依赖于 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(); }
}
Neo4j 连接的配置class
@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(); } }
用 NodeEntity
注释你的国家 class@NodeEntity public class Country{ public int id; public String name; public String locale; }
使用 Neo4jTemplate 将对象保存到 Graph DB
@Autowired Neo4jTemplate template; public Country save(Country country){ return template.save(country); }