我正在使用 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”并保存。然后我将值编辑为“ROLE_SYSTEM_OWNER”并再次调用 save()。
在 Neo4j 查询浏览器中,它只显示“ROLE_SYSTEM_OWNER”,目前一切正常。
但是,当我调用 findAll() 时,所有者有两个值 ["ROLE_ADMIN","ROLE_SYSTEM_OWNER"]
当我重新启动我的网络服务器时它会正常工作。
【改变价值的方法】
@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]]
}