[英]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.