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