简体   繁体   中英

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

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

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=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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM