javaneo4jspring-data

How to map element ID to DTO object field?


I'm trying to map the elementId value in the DTO class field in Spring Data, but the mapping doesn't happen. If I change the field name to something other than id, the mapping is performed.

    @Query("""
        MATCH (s:Skill {id:$skillId})-[r]-(ss:Synonym)
        RETURN elementId(ss) AS id,
               ss.synonym AS synonym,
               ss.autocomplete AS autocomplete
    """)
    Collection<SynonymResponse> findSynonymsBySkill(String skillId);
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class SynonymResponse {
    /** ID. */
    private String id;
    /** Tittle. */
    private String synonym;
    /** Show in autocomplete. */
    private boolean autocomplete;
}
@Node({"Synonym"})
public class Synonym {
    @Id
    @GeneratedValue
    private Long id;
    @JsonProperty(
        required = true
    )
    @Property("synonym")
    private String title;
    private boolean autocomplete;

I tried changing the name field in the DTO, but when the name is different, the display occurs.


Solution

  • I can produce this solution way shown below

    By using @JsonProperty, it can help clarify which JSON property maps to which field in SynonymResponse

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Getter
    @Setter
    public class SynonymResponse {
        @JsonProperty("id") // Ensure this matches what you want to map
        private String id;
        private String synonym;
        private boolean autocomplete;
    }