簡體   English   中英

為什么@RelatedToVia具有可選的type和elementClass參數?

[英]Why does @RelatedToVia have optional type and elementClass parameters?

@RelatedToVia在彈簧數據Neo4j的注釋一個上的場NodeEntity對象作為一個參考@RelationshipEntity連接該節點到另一個節點的對象。 相反, @RelatedTo將字段標記為對已連接節點本身的引用,並且具有可選的type參數,該參數可用於指定兩個節點之間的關系類型。

@RelationshipEntity(type="SOME_LINK")
class SomeLink { 
    @StartNode 
    FooNode foo;

    @EndNode 
    BarNode bar;
}

@NodeEntity
class FooNode {
    @RelatedTo(type="SOME_LINK")
    BarNode bar;

    @RelatedToVia(type="SOME_LINK") //what's the point in this annotation? 
    SomeLink link; 
}

@RelatedToVia具有相同的可選type參數,我很好奇為什么會這樣:關系的類型在字段類型中指定(用@RelatedToVia注釋的字段必須鍵入@RelationshipEntity -annotated類,該類必須指定一種要編譯的類型),那么在注釋參數中指定它的意義何在?

更令人困惑的是, @RelatedToVia還有一個可選的elementClass參數,根據文檔,該參數“返回目標關系實體類”- 正是在字段類型中指定的內容。

@RelatedToVia(elementClass=SomeLink.class)  //what's the point? 
SomeLink link; 

@RelatedToVia(elementClass=SomeOtherLink.class)  // ???? 
SomeLink link; 

我很好奇,因為我有預感,這是為了通過RelationshipEntity類和RelatedToVia字段啟用一些有用的多態行為,並且我很樂意看到一個示例,說明如何實現這種行為。 另外,使用兩種不同的注釋可以實現哪些不同類型的行為?

同樣,似乎奇怪的是,這兩個參數都存在(可能是多余的)以指定關系的類型,而不存在用於指定關系另一端的節點的類的參數-可以但不必聲明在RelationshipEntity類中,這對我幾次有用。 為什么會這樣呢?

這個:

@RelatedToVia(type="SOME_LINK")

優先於

@RelationshipEntity(type="SOME_LINK")

@RelatedToVia(elementClass=SomeOtherLink.class)  // this is indeed redundant
SomeLink link;

// this is NOT redundant, you lose the type information in runtime in Java
// you only know that link is a set, don't know of which type
@RelatedToVia(elementClass=SomeOtherLink.class)      
Set<SomeLink> link;

設置關系的多種方法可為您提供靈活性。 您可以將相同的關系類重用於具有相同屬性的多個不同的關系類型。

在參考文檔中查看更多

http://docs.spring.io/spring-data/data-neo4j/docs/3.1.4.RELEASE/reference/html/programming-model.html#reference_programming_model_relationships_relationshiptypeprecedence

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM