简体   繁体   English

为什么@RelatedToVia具有可选的type和elementClass参数?

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

@RelatedToVia in Spring-Data-Neo4j annotates a field on a NodeEntity object as a reference to a @RelationshipEntity object connecting that node to another node. @RelatedToVia在弹簧数据Neo4j的注释一个上的场NodeEntity对象作为一个参考@RelationshipEntity连接该节点到另一个节点的对象。 @RelatedTo , in contrast, marks the field as a reference to the connected node itself, and has an optional type parameter that can be used to specify the type of relationship between the two nodes. 相反, @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 has the same optional type parameter, and I'm curious about why that is: The type of relationship is specified in the field type (a field annotated with @RelatedToVia must be typed to a @RelationshipEntity -annotated class, which must specify a type to compile), so what would be the point of specifying it in the annotation parameter? @RelatedToVia具有相同的可选type参数,我很好奇为什么会这样:关系的类型在字段类型中指定(用@RelatedToVia注释的字段必须键入@RelationshipEntity -annotated类,该类必须指定一种要编译的类型),那么在注释参数中指定它的意义何在?

Even more confusingly, @RelatedToVia also has an optional elementClass parameter that, according to the docs, "returns the target relationship entity class" - which is exactly what's specified in the field type. 更令人困惑的是, @RelatedToVia还有一个可选的elementClass参数,根据文档,该参数“返回目标关系实体类”- 正是在字段类型中指定的内容。

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

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

I'm curious because I have a hunch that this was intended to enable some useful polymorphic behavior with RelationshipEntity classes and RelatedToVia fields, and I'd love to see an example of how such behavior might be implemented. 我很好奇,因为我有预感,这是为了通过RelationshipEntity类和RelatedToVia字段启用一些有用的多态行为,并且我很乐意看到一个示例,说明如何实现这种行为。 As a bonus, what different sorts of behaviors can be achieved using the two different annotations? 另外,使用两种不同的注释可以实现哪些不同类型的行为?

Also, it seems curious that both these parameters exist for (possibly redundantly) specifying the type of the relationship, while no parameter exists for specifying the class of the node on the other end of the relationship - which can be but isn't necessarily declared in the RelationshipEntity class, and which would have been useful to me on a couple of occasions. 同样,似乎奇怪的是,这两个参数都存在(可能是多余的)以指定关系的类型,而不存在用于指定关系另一端的节点的类的参数-可以但不必声明在RelationshipEntity类中,这对我几次有用。 Why might that be? 为什么会这样呢?

This: 这个:

@RelatedToVia(type="SOME_LINK")

has precedence over 优先于

@RelationshipEntity(type="SOME_LINK")

and

@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;

Having multiple ways to set the relationship gives you flexibility. 设置关系的多种方法可为您提供灵活性。 You can reuse the same relationship class for multiple different relationship types that have same properties. 您可以将相同的关系类重用于具有相同属性的多个不同的关系类型。

See more in reference docs 在参考文档中查看更多

http://docs.spring.io/spring-data/data-neo4j/docs/3.1.4.RELEASE/reference/html/programming-model.html#reference_programming_model_relationships_relationshiptypeprecedence 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.

相关问题 在Java中,我可以有多个有界类型参数,但是是可选的 - In Java Can I have Multiple Bounded Type Parameters But Be Optional 有多个可选参数 - Have multiple optional parameters 使用Retrofit2时,为什么会出现“类型okhttp3.Call没有类型参数”? - Why do I get “Type okhttp3.Call does not have type parameters” when using Retrofit2? 为什么Optional或flatMap方法的供应商类型参数是通配符? - Why are Optional's or and flatMap methods' supplier type parameters wildcards? 为什么类型参数必须重复两次? - Why do the type parameters have to be repeated twice? 为什么Netbeans中的“类型列表不带参数”? - Why “type List does not take parameters” in Netbeans? 为什么可选的水槽通道会导致非可选的水槽通道出现问题? - Why does an optional flume channel cause a non-optional flume channel to have problems? 为什么Array元素必须属于同一类型 - Why does Array elements have to be of the same type 为什么 ManagedChannelBuilder 没有用于与服务器建立 TLS 连接的 TLS 参数? - Why does ManagedChannelBuilder not have TLS parameters for making TLS connections to the server? 为什么编译?代码似乎打破了类型参数的约束 - Why does this compile? The code seems to be breaking constraints on the type parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM