Multiple relationship classes with the same type

Using spring -data-neo4j, I want to create two classes using @RelationshipEntity(type="OWNS") to associate the Person class with both Pet and Car .

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

This saves the Graph database correctly without any problems, as I can query the actual Node and Relationship and see their type , etc.

But when I try to use @RelatedTo(type="OWNS", elementClass=Pet.class) , I either get a class exception, or when using lazy initialization I get incorrect results.

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

The result that I get when I try to print my my man (my toString() was omitted, but it just calls toString() for each field):

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

Does anyone know if this can be done, it must be done, is it just a bug or a missing function?

+6
source share
1 answer

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

Unfortunately, this will cause a problem if you use a different @RelatedTo annotation with a lot of pets or cars associated with another annotation. Since this will not be different from "OWNS" and any other relationship to the Pet-Type, the set returns all related pet animals (for example: peter β†’ (HATES-Relationsip) β†’ dogs).

If this is a mistake or not, I can’t say ... But for the database: There are only nodes and relationships. Both are not dialed, so neo4j knows nothing about your "Pet'-" or "Car'-Class". Spring data neo4j handles this by indexing all nodes on a type and setting the type attribute, or using a specific mock graph (with subrections). Even if you want to get all the person’s pets with a workaround description, you will have much more code to write, because the outgoing relationship with the name β€œOWNS” contains two types of objects.

I would recommend using two different names. It's easier to write your custom workarounds / queries later and probably even faster because class type mapping is not required. Is there a reason why you need these specific names?

PS: It is possible that not all are 100% accurate. I do not know springdataneo4j in detail, but this is what I have guessed so far.

+3
source

Source: https://habr.com/ru/post/916716/


All Articles