6

使用 spring-data-neo4j,我想创建两个类,@RelationshipEntity(type="OWNS")用于将一个Person类链接到 aPetCar.

@RelationshipEntity(type="OWNS")
public class OwnsCar {  
    @Indexed
    private String name;

    @StartNode
    private Person person;

    @EndNode
    private Car car;
}

@RelationshipEntity(type="OWNS")
public class OwnsPet {
    @Indexed
    private String name;

    @EndNode
    private Person person;

    @StartNode
    private Pet pet;
}

这可以正确保存到图形数据库中,没有问题,因为我可以查询实际NodeRelationship查看它们的类型等。

但是当我尝试使用时,@RelatedTo(type="OWNS", elementClass=Pet.class)我要么得到一个类转换异常,要么在使用延迟初始化时得到不正确的结果。

@NodeEntity
public class Person {   
    @Indexed
    private String name;

    @RelatedTo(type="OWNS", direction=Direction.OUTGOING, elementClass=Pet.class)
    private Set<Pet> pets;

    @RelatedTo(type="OWNS", direction=Direction.OUTGOING, elementClass=Car.class)
    private Set<Car> cars;
} 

当我尝试打印我的人时得到的结果(我的toString()已被省略,但它只是toString()为每个字段调用)是这样的:

Person [nodeId=1, name=Nick, pets=[Car [nodeId=3, name=Thunderbird]], cars=[Car [nodeId=3, name=Thunderbird]]]

有谁知道这是否可以完成,应该完成,只是一个错误或缺少的功能?

4

1 回答 1

3

It seems like the problem is, that the annotation causes springDataNeo4j to priorize the relationship name. I tried the same on another sample I created. If both annotations contain type="OWNS" it mixes both 'objects'. When I omit this information, and only use direction and type, it works for me.

Unfortunately this will lead to a problem if you are using another @RelatedTo annotation with more Pets or Cars related with another annotation. As it would not differ between "OWNS" and any other relation to a Pet-Type, the set returns all related pets (example: peter ->(HATES-Relationsip)->dogs).

If it's a bug or not, I can't tell... But for the database: There are only nodes and relations. Both are not typed, so neo4j does not know anything about your 'Pet'- or 'Car'-Class. Spring data neo4j handles this, either by indexing all nodes per type and setting a type-attribute, or using a specific graph-layout (with subreferences). Even if you would want to fetch all pets of a person with a traversal description, you would have so much more code to write, since the outgoing relations with name 'OWNS' contains two types of objects.

I would recommend using two different names. It's easier to write your custom traversals/queries later on, and its probably even faster as well, because no class-type comparison will be needed. Is there any reason, why you would need these specific names?

PS: It is possible, that not everything is 100% accurate. I don't know springdataneo4j in detail, but that's what I figured out so far.

于 2012-06-04T15:50:44.117 回答