2014-07-10 23 views
1

我要的很簡單:一個節點ID驗證。春季如何處理換位回滾異常?

這個項目的結構: 控制器(終點) - 服務 - DAO。服務層中的@transaction,服務層中的驗證。

在端點,通在DTO對象。驗證服務層中的節點ID並將其保存到DAO層中的neo4j數據庫中。在DTO對象(例如學生)中,我傳入用於建立學生和班級關係的另一個節點ID(例如Class)。在保存之前,我會對節點(Class)ID進行驗證。

如果我使用repository.findOne(id)並傳遞存在於數據庫中,但錯誤的類型的節點ID。這將提高:org.neo4j.graphdb.NotFoundException: '__type__' property not found for RelationshipImpl #10118 of type 36 between Node[7054] and Node[6726]這將導致以下異常:如果我使用下面的查詢

nested exception is org.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is javax.transaction.RollbackException: Failed to commit, transaction rolledback 

,並通過在不數據庫

start node1 = node(id) 
return node1 

org.neo4j.cypher.EntityNotFoundException存在節點ID將會引發並導致UnexpectedRollbackException。

有什麼辦法我捕獲這些異常並沒有UnexpectedRollbackException返回null還是假的?

或者是有什麼辦法可以檢查節點ID存在於數據庫容易嗎?

+0

01)混淆你的 「要求」,你會分享方法聲明,@Transactional,誰是throwin g例外? 02)您正在接收空對象或異常? –

+0

也許你可以分享更多的代碼?例如,你可以使用'repository.findOne(id)' –

回答

0

修復此使用:

  1. For節點:

     Node node = neo4jTemplate.getNode(id); 
         String type = (String) node.getProperty("__type__"); 
         if (!type.equals(nodeClass.getName())) { 
          throw new ResultNotFoundException("No such object (" + nodeClass.getSimpleName() + ": id=" + id + ") in database"); 
         } 
         return neo4jTemplate.projectTo(node, nodeClass); 
    
  2. 對於關係

     Relationship relationship = neo4jTemplate.getRelationship(id); 
         String type = (String) relationship.getProperty("__type__"); 
         if (!type.equals(nodeClass.getName())) { 
          throw new ResultNotFoundException("No such object (" + nodeClass.getSimpleName() + ": id=" + id + ") in database"); 
         } 
         return neo4jTemplate.projectTo(relationship, nodeClass);