1

我有以下实体:

@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

4

1 回答 1

2

minCreateDate您用于查找方法的参数是日期类型,createDate属性是字符串。所以,这部分a.createDate <= {minCreateDate}基本上是比较 String 的表示minCreateDate和 String 的属性createDate

在我的项目中,我通常将日期和时间戳保存在数据库和我的代码中。

或者更好的是,如果日期属性对我的应用程序至关重要,我将使用“时间树模型”方法:https ://graphaware.com/neo4j/2014/08/20/graphaware-neo4j-timetree.html

于 2017-10-09T13:38:09.070 回答