2016-09-20 40 views
1

我想使用neo4j在數據庫級別計算我的域對象的一些屬性並返回只讀結果。在JPA可以在域對象實體的字段,通過@Formula註釋實現這一目標:來自JPA for Spring數據neo4j的@Formula註解的模擬?

@Formula("(select avg(f.rating) from Feedback f where f.offer_id = offer_id)") 
private Double rating; 

我應該做一個實現在Spring數據的Neo4j相同的行爲?我寫了一個Cypher查詢,但不知道在哪裏使用它。

回答

4

類似的結果可以用@QueryResult

  • 創建領域類來保存返回的數據來實現。
  • 與標註它@QueryResult

實施例:(在科特林,這是我什麼手邊)

@QueryResult 
open class Principal constructor(applicationToken: String, 
          profileId: String, 
          stageName: String, 
          showMeLaterDays: Float, 
          roles: Array<Role>) 
{ 

    var applicationToken: String 
    var profileId: String 
    var stageName: String 
    var showMeLaterDays: Float 

    @Convert(RoleArrayAttributeConverter::class) 
    var roles: Array<Role> 



    init 
    { 
     this.applicationToken = applicationToken 
     this.profileId = profileId 
     this.stageName = stageName 
     this.showMeLaterDays = showMeLaterDays 
     this.roles = roles 
    } 

    //Provide a default constructor for OGM 
    constructor() : this(applicationToken = "", profileId = "", stageName = "", showMeLaterDays = 0f, 
     roles = emptyArray()) 
} 

然後用庫用它作爲如下:

@Query("MATCH (n:CandidateProfile {applicationToken: {0} }) 
    RETURN n.id as profileId, n.applicationToken as applicationToken, n.stageName as stageName, n.showMeLaterDays as showMeLaterDays, n.roles as roles;") 
fun findByApplicationToken(token: String): Principal? 
  • 請注意節點屬性返回與類字段名稱對應的方式。
  • 功能結果也可以做到這一點。
+0

注意:更多地道的Kotlin POJO請參考:https://github.com/neo4j-examples/movies-kotlin-spring-data-neo4j –