3
我已經將一些節點插入Neo4j DB.And我想從數據庫中選擇一些節點並將其轉換爲特定的類。Neo4j:spring-data-neo4j,如何將結果轉換爲我的類型類?
以下是關於這個問題的一些代碼:
+-------------------------------------------------------------------------+
| Node[11]{career:"programmer",name:"kelvin",age:35,introduce:"lazy doy"} |
+-------------------------------------------------------------------------+
@NodeEntity
class User {
@GraphId
Long id;
String name;
int age;
}
我只是想從數據庫獲取節點的ID,姓名,年齡,並把它變成:
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)
}
}
關於類似節點的細節用戶類。但許多方法都失敗了。
這裏我遇到了一個問題,那就是:如何將結果投射到我的目標類中?我嘗試了很多方法來投射但最後失敗。感謝您的關注。
好吧,它現在可以工作。 非常感謝。 我嘗試過以下方法來實現它。 結果結果= neo4jTemplate.query(「start n = node(11)return ID(n),n.name,n.age;」,null) List list = result.collect {new User(it) } 兩種方法可以滿足我的要求。 最後,再次感謝。 –
你也可以使用:'result.to(User.class).as(List.class)' –