[英]Neo4j: spring-data-neo4j,how to cast result to my type class?
我已经在Neo4j DB中插入了一些节点。我想从数据库中选择一些节点并将其转换为特定的类。
以下是有关该问题的一些代码:
class Service {
Neo4jTemplate neo4jTemplate
@Transactional
def find() {
def id1 = 11
//Knowledge k = neo4jTemplate.findOne(1, Knowledge)
Result result = neo4jTemplate.query("start n=node(11) return ID(n),n.name,n.age;", null)
//how to cast the result to User class
println "the tpye of result called User is "+ result.to(User.class).is(cn.edu.bnuz.itc.bok.sub2.User.class)
}
}
节点的细节如下:
+-------------------------------------------------------------------------+
| Node[11]{career:"programmer",name:"kelvin",age:35,introduce:"lazy doy"} |
+-------------------------------------------------------------------------+
@NodeEntity
class User {
@GraphId
Long id;
String name;
int age;
}
我只想从db获取节点的id,name,age并将其放入User类。 但很多时候用很多方法失败了。
在这里我遇到了一个问题:如何将结果转换为目标类? 我尝试了许多方法但最终失败了。谢谢你的关注。
从查询中返回用户节点,并使用所需的类作为参数调用返回Result
的to
方法:
Result result = neo4jTemplate.query("start n=node(11) return n", null);
for(User u : result.to(User.class)) {
doSomethingWith(u);
}
您可能需要考虑使用 支持cypher查询的 存储库 ,例如:
public interface UserRepository extends GraphRepository<User> {
@Query("start n=node(11) return n")
Iterable<User> getUser11();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.