spring-data-neo4j icon indicating copy to clipboard operation
spring-data-neo4j copied to clipboard

Cannot Properly mappig when use custom data types

Open Sternstunde34 opened this issue 1 year ago • 1 comments
trafficstars

In my Project,I use theCQL below:

@Query("MATCH (c:Concept)-[r]-(related:Concept) WHERE c.name = $name RETURN c AS concept, collect({id:ID(r),relation:type(r),relatedConcept:related}) AS ccLinks, collect(related) AS relatedConcepts")
    List<ConceptGraphDto> findConceptWithRelationsByName(@Param("name") String name);

I got null response on 'ccLinks' field like this:

[{"concept":{"id":1,"name":"DSA算法","englishDefinition":null,"chineseDefinition":null,"conceptLevel":null,"englishName":null,"originator":null,"originTime":null,"originThesis":null},"ccLinks":[{"id":null,"relatedConcept":null,"relation":"从属关系"},{"id":null,"relatedConcept":null,"relation":"变种关系"}],"relatedConcepts":[{"id":4,"name":"非对称加密","englishDefinition":null,"chineseDefinition":null,"conceptLevel":null,"englishName":null,"originator":null,"originTime":null,"originThesis":null},{"id":2,"name":"ElGamal算法","englishDefinition":null,"chineseDefinition":null,"conceptLevel":null,"englishName":null,"originator":null,"originTime":null,"originThesis":null}]}]

I customly defined ConceptGraphDto like this:

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Data
public class ConceptGraphDto {
    private Concept  concept;
    private List<CCLink> ccLinks;
    private List<Concept> relatedConcepts;
}

And my CCLink class:

@Data
@RelationshipProperties
public class CCLink implements Serializable {
    @RelationshipId
    @GeneratedValue
    private Long id;

    @TargetNode
    private Concept relatedConcept;

    @Property
    private String relation;


    public CCLink(Concept relatedConcept, String relation) {
        this.relatedConcept = relatedConcept;
        this.relation = relation;
    }
}

When I try another CQL :

@Query("MATCH (c:Concept)-[r]-(related:Concept) WHERE c.name = $name RETURN collect({id:ID(r),relation:type(r),relatedConcept:related})")
    List<CCLink> test(@Param("name") String name);

I can properly get correct resopnse. Apparently maybe there's somrthing wrong in the first CQL.Help plz!

Sternstunde34 avatar May 09 '24 09:05 Sternstunde34

I am not really sure what your domain looks like and what you are trying to get with those queries. Unrelated to your queries, SDN should not be used to create wrapper objects like your ConceptGraphDto. I cannot only assume that your custom queries are in a ConceptRepository or similar. If you want to do such things, I recommend using the Neo4jClient and use the mapper for this entity. https://docs.spring.io/spring-data/neo4j/reference/appendix/neo4j-client.html#neo4j-client.result-objects.mapping-functions Even though the example in the documentation uses the reactive client, it is also possible with the imperative one. Would be good to know if I guessed right here.

My second note is about mapping relationship properties directly. A relationship should never get queries alone. Even if this works in your code, it is an unsupported "feature". Spring Data Neo4j's entities are Nodes and not the associations between them.

meistermeier avatar May 14 '24 15:05 meistermeier

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar May 21 '24 15:05 spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-projects-issues avatar May 28 '24 15:05 spring-projects-issues