2

在我Neo4j 3.0.1SDN 4.1.1.RELEASE項目,我有以下實體:Neo4j的3.0.1 SDN 4.1.1.RELEASE幻象節點

@NodeEntity 
public class CriterionGroup extends Authorable { 

    private final static String DEFINED_BY = "DEFINED_BY"; 
    private final static String CONTAINS = "CONTAINS"; 

    private String name; 

    private String description; 

    @Relationship(type = DEFINED_BY, direction = Relationship.OUTGOING) 
    private Decision owner; 

    @Relationship(type = CONTAINS, direction = Relationship.OUTGOING) 
    private Set<Criterion> criteria = new HashSet<>(); 
.... 

@NodeEntity 
public class Criterion extends Authorable { 

    private final static String CONTAINS = "CONTAINS"; 
    private final static String DEFINED_BY = "DEFINED_BY"; 

    private String name; 

    private String description; 

    @Relationship(type = CONTAINS, direction = Relationship.INCOMING) 
    private CriterionGroup group; 

    @Relationship(type = DEFINED_BY, direction = Relationship.OUTGOING) 
    private Decision owner; 
.... 


@NodeEntity 
public class Decision extends Commentable { 

    private final static String CONTAINS = "CONTAINS"; 
    private final static String DEFINED_BY = "DEFINED_BY"; 
    private final static String VOTED_FOR = "VOTED_FOR"; 

    private String name; 

    @Relationship(type = CONTAINS, direction = Relationship.INCOMING) 
    private Set<Decision> parentDecisions = new HashSet<>(); 

    @Relationship(type = CONTAINS, direction = Relationship.OUTGOING) 
    private Set<Decision> childDecisions = new HashSet<>(); 

    @Relationship(type = DEFINED_BY, direction = Relationship.INCOMING) 
    private Set<CriterionGroup> criterionGroups = new HashSet<>(); 

    @Relationship(type = DEFINED_BY, direction = Relationship.INCOMING) 
    private Set<Criterion> criteria = new HashSet<>(); 
.... 

在我的測試我試圖用以下方法庫中刪除CriterionGroup

@Query("MATCH()-[r]-(cg:CriterionGroup) WHERE id(cg) = {criterionGroupId} DELETE cg, r") 
void deleteCriterionGroup(@Param("criterionGroupId") Long criterionGroupId); 

然後我試圖通過ID來獲得這個CriterionGroup

criterionGroupRepository.findOne(id); 

並且它返回NULL。到現在爲止還挺好。

緊接着,我試圖擺脫Criterion組對象是在被刪除CriterionGroup並返回..刪除CriterionGroup

criterionRepository.findOne(criterion.getId()).getGroup() 

我在做什麼錯?一切工作正常SDN 3.4.4.RELEASENeo4j 2.3.3Neo4j 3.0.1SDN 4.1.1.RELEASE由於我知識有限,我有很多意想不到的情況。

此外,有好有以下關係定義在一個實體(我已刪除enforceTargetType

@Relationship(type = CONTAINS, direction = Relationship.INCOMING) 
private Set<Decision> parentDecisions = new HashSet<>(); 

@Relationship(type = CONTAINS, direction = Relationship.OUTGOING) 
private Set<Decision> childDecisions = new HashSet<>(); 

他們有不同的方向。

回答

2

據我所知,您將使此CriterionGroup重新出現,因爲您通過自定義查詢刪除了它,因此繞過了OGM。該圖知道你的變化,但OGM的映射上下文不知道。 解決方案是在使用Session.detachEntity(id)通過自定義查詢刪除此實體後,從會話中註銷此實體,或者使用session.clear()刷新整個會話。

是的,它是好的,有CONTAINS關係的定義,請記住,標誌着INCOMING任何關係,如果他們@Relationship(type = CONTAINS, direction = Relationship.INCOMING)

更新存在,你必須註解存取,存取器和屬性parentDecisions:另外,還要確保您的對象模型與您通過自定義查詢刪除的內容同步。即如果您要通過自定義查詢刪除CriterionGroup,則還應更新對象模型以反映此情況,即criterion.group=null。或者,使用session.clear()完全清除會話並重新加載所有相關實體。

+0

感謝您的回答。獲得對Session對象的引用的正確方法是什麼?另外,對於'Decision.parentDecisoins'我有以下方法 - '設置 getParentDecisions()','addParentDecision(決策決定)','removeParentDecision(決策決定)'..應該所有的人也可以用'@Relationship註解(type = CONTAINS,direction = Relationship.INCOMING)'?如果是這樣,爲什麼我們需要這種冗餘? – brunoid

+0

我找到了一個如何訪問Session的方法。但'session.detachNodeEntity(criterionGroupId);'不起作用。但'session.clear();'也清除所有其他對象,所以我現有的所有測試都失敗了。 – brunoid

+0

另外,您是否有計劃自動同步OGM和自定義Cypher查詢執行之間的狀態? – brunoid