Home > Net >  No property named "Id" exists in source parameter(s). Did you mean "null"?
No property named "Id" exists in source parameter(s). Did you mean "null"?

Time:01-31

I'm kind of stuck in below error

Post.class

@Data
@Getter
@Setter
@Entity
@Table(name = "posts", uniqueConstraints = { @UniqueConstraint(columnNames = { "title" }) }

)

public class Post {

    @Id
    @Column(name = "Id", nullable = false)
    private Long Id;

    @Column(name = "title", nullable = false)
    private String title;

    @Column(name = "description", nullable = false)
    private String description;

    @Column(name = "content", nullable = false)
    private String content;

    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Comment> comments = new HashSet<>();

Comment.class

    Data
    @Entity
    @Table(name = "postcomment")
    public class Comment {
    
        @Id
        @Column(name = "Id", nullable = false)
        private Long Id;
    
        @Column(name = "remark", nullable = false)
        private String remark;
    
        @ManyToOne(fetch = FetchType.LAZY, optional = false) // get only related comments to post
        @JoinColumn(name = "post_Id", nullable = false)
        @OnDelete(action = OnDeleteAction.CASCADE)
        private Post post;
    }

PostDto .class

@Data
public class PostDto {

    private Long Id;

    @NotEmpty
    @Size(min = 2, message = "post title should have atleast 2 characters")
    private String title;

    @NotEmpty
    @Size(min = 10, message = "post title should have atleast 2 characters")
    private String description;

    @NotEmpty
    @Size(min = 10, message = "post title should have atleast 2 characters")
    private String content;

CommentDto.class

@Data
public class CommentDto {

    private Long Id;
    private String remark;


}

CommentMapper.class

@Mapper(imports = { Instant.class, DateTimeFormatter.class })
@Configuration
public interface CommentMapper {

    CommentMapper INSTANCE = Mappers.getMapper(CommentMapper.class);

    @Mapping(source = "Id", target = "Id")
    @Mapping(source = "remark", target = "remark")
    Comment getComment(CommentDto postDto);

    @Mapping(source = "Id", target = "Id")
    @Mapping(source = "remark", target = "remark")
    CommentDto getCommentDto(Comment post);

}

I'm trying to solve it from last 6 hours, but still not getting why its showing me below error

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project rest.api: Compilation failure: Compilation failure: 
[ERROR] /D:/work/rest.api/src/main/java/com/rest/api/mapper/CommentMapper.java:[22,17] No property named "Id" exists in source parameter(s). Did you mean "null"?
[ERROR] /D:/work/rest.api/src/main/java/com/rest/api/mapper/CommentMapper.java:[22,17] No property named "remark" exists in source parameter(s). Did you mean "null"?
[ERROR] /D:/work/rest.api/src/main/java/com/rest/api/mapper/CommentMapper.java:[26,20] No property named "Id" exists in source parameter(s). Did you mean "null"?
[ERROR] /D:/work/rest.api/src/main/java/com/rest/api/mapper/CommentMapper.java:[26,20] No property named "remark" exists in source parameter(s). Did you mean "null"?

Any helpful suggestion will be really a hope to fix this.

Secondly, Is it okay to have only @Data, when need all getters and setters ?

CodePudding user response:

The reason why you are getting the error: "No property named "Id" exists in source parameter(s). Did you mean "null"?" is due to the fact that a property names Id does not exist.

Most likely you have a getter / setter that look like getId() and setId(String Id).

This means that from MapStruct point of view the name of the property is id. MapStruct does not look into private fields, it detects properties based on getters / setters and not field.

  •  Tags:  
  • Related