我正在使用事务事件处理程序对已删除的节点进行安全检查,以确保当前用户是否被允许这样做。
为了确保我有正确的节点来检查我首先必须找出它是否具有某个属性,然后检查另一个属性的值,所以理想情况下代码应该是这样的:
graphDatabaseService.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Object>() {
@Override
public Object beforeCommit(TransactionData data) throws Exception {
for (Node node : data.deletedNodes()) {
if (node.hasProperty("checkProperty")){
if (node.hasProperty("propertyToCheck")){
String value = (String) node.getProperty("propertyToCheck");
... do checking on value
}
}
}
return null;
}
但这失败了,但有例外
Caused by: java.lang.IllegalStateException: Node[11] has been deleted in this tx
at org.neo4j.kernel.impl.core.WritableTransactionState$CowEntityElement.assertNotDeleted(WritableTransactionState.java:141)
at org.neo4j.kernel.impl.core.WritableTransactionState$CowEntityElement.getPropertyAddMap(WritableTransactionState.java:129)
at org.neo4j.kernel.impl.core.WritableTransactionState$CowNodeElement.getPropertyAddMap(WritableTransactionState.java:155)
at org.neo4j.kernel.impl.core.WritableTransactionState.getCowPropertyAddMap(WritableTransactionState.java:529)
at org.neo4j.kernel.impl.core.Primitive.hasProperty(Primitive.java:306)
at org.neo4j.kernel.impl.core.NodeImpl.hasProperty(NodeImpl.java:53)
at org.neo4j.kernel.impl.core.NodeProxy.hasProperty(NodeProxy.java:160)
我发现的唯一解决方法是:
for (Node node : data.deletedNodes()) {
boolean check = false;
String valueToCheck = null;
for (PropertyEntry prop : data.removedNodeProperties()) {
if (node.equals(prop.entity())) {
if (prop.key().equals("checkProperty")) {
check = true;
}
if (prop.key().equals("propertyToCheck")) {
valueToCheck = (String) prop.previouslyCommitedValue();
}
}
}
if (check){
... do checking on value
}
}
但这会遍历所有已删除的属性,因此这显然不是一个好的解决方案。
所以我的问题是:有没有更好的方法来做到这一点?
使用 Neo4j 1.9.3