1

比較java.util.Date我有以下實體:SDN5/OGM3在Cypher支架查詢

@NodeEntity 
public class Action { 

... 

    @Index(unique = false) 
    private Date createDate; 

... 

} 

我需要這是在一定的時間較前期創建的最後一個Action

爲了做到這一點,我已經實現了以下系統信息庫方法:

@Repository 
public interface ActionRepository { 

    @Query("MATCH (a:Action)-[:CREATED_BY]->(u:User) WHERE a.entityType = {entityType} AND a.createDate <= {minCreateDate} AND u.id = {userId} RETURN a ORDER BY a.createDate DESC LIMIT 1") 
    Action findLastByEntityTypeForUser(@Param("entityType") String entityType, @Param("minCreateDate") Date minCreateDate, @Param("userId") Long userId); 

} 

我用下面的代碼來測試這個方法:

decisionDao.create("Decision2", "Decision2 description", null, false, null, user1); 

Date minStartDate = DateUtils.addMilliseconds(new Date(), -1000 * 60); 

Action user1LastAction = actionRepository.findLastByEntityTypeForUser(Decision.class.getSimpleName(), minStartDate, user1.getId()); 

assertNotNull(user1LastAction); // test fails here because of NPE 

但沒有Cypher支架查詢AND a.createDate <= {minCreateDate}的這部分我可以成功找到Action實例。

在Neo4j的水平我的數據是這樣的:

{ 
    "updateDate":"2017-10-08T12:21:39.15 
3Z", 
    "entityName":"General", 
    "en 
tityType":"CriterionGroup", 
    "entityId":1, 
    "id":1, 
    "type":"CREATE", 
    "createDate":"2017-10-08T12:21:39.153Z" 
} 

我在做什麼錯誤以及如何正確比較SDN/OGM和Cypher支架的日期?

此外,有什麼辦法告訴SDN/OGM存儲java.util.Date對象爲long毫秒和String

回答

2

minCreateDate您用於find-Method的參數的類型爲Date,createDate屬性爲String。所以,這部分a.createDate <= {minCreateDate}基本上比較minCreateDate的字符串表示和字符串屬性createDate

在我的項目中,我通常將日期和時間戳保存在數據庫和我的代碼中。

甚至更​​好,如果日期屬性是我的應用程序是至關重要的,我使用的是「時間樹模型」的方法:https://graphaware.com/neo4j/2014/08/20/graphaware-neo4j-timetree.html

+1

謝謝您的回答。有什麼辦法可以告訴SDN/OGM將'java.util.Date'對象保存爲'long'毫秒? – alexanoid

+0

不是我的知識 –

+2

是的你可以: '@Index(unique = false) @DateLong public Date createDate;'''' – nmervaillie