NodeEntity缓存Set的值



我使用的是Spring Data Neo4j 4。似乎Neo4j的"PersistenceContext"缓存了"Set"值的值。

的实体
@NodeEntity
public class ServiceStatus implements java.io.Serializable {
    @GraphId Long id;
    private Set<String> owners = new HashSet<String>();
}

首先,我将值"ROLE_ADMIN"放在owner中并保存它。然后我将值编辑为"ROLE_SYSTEM_OWNER"并再次调用save()。

在Neo4j查询浏览器中,它只显示"ROLE_SYSTEM_OWNER",这现在是正确的。

然而,当我调用findAll()时,所有者有两个值["ROLE_ADMIN","ROLE_SYSTEM_OWNER"]

它将工作良好,当我重新启动我的web服务器。

[更改值的方法]

@Test
public void testSaveServiceStatus() throws OSPException {
    //1. save 
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.setServiceName("My Name");
    Set<String> owners = new HashSet<String>();
    owners.add("ROLE_SITE_ADMIN");
    serviceStatus.setOwners(owners);
    serviceStatusRepository.save(serviceStatus);
    System.out.println(serviceStatus.getId()); //262
}
@Test
public void testEditServiceStatus() throws OSPException{
    //1. to find all , it seems cache the set value
    serviceStatusRepository.findAll();

    //2. simulate the web process behavior
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.setId(new Long(262));
    serviceStatus.setServiceName("My Name");
    Set<String> owners = new HashSet<String>();
    //change the owner to Requestor
    owners.add("Requestor");
    serviceStatus.setOwners(owners); 
    //3. save the "changed" value 
    // In the cypher query browser, it show "Requestor" only
    serviceStatusRepository.save(serviceStatus);
    //4. retrieve it again
    serviceStatus = serviceStatusRepository.findOne(new Long(262));
    System.out.println(serviceStatus); //ServiceStatus[id=262,serviceName=My Name,owners=[Requestor5, Requestor4]]
}

您的测试似乎在某种程度上与分离对象一起工作。第一步,findAll()将这些实体加载到会话中,但随后的第2步不是使用加载的实体,而是创建一个随后保存的新实体。"附加的"实体仍然指的是实体的早期版本。OGM目前不处理这个

你最好修改实体加载在findAll或只是一个findOne(id),修改,保存(而不是通过设置id重新创建一个)。

最新更新