1

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 ?

Oxana Grey
  • 327
  • 2
  • 12
  • You still need getters / setters, I believe. So you should add `@Getter` and `@Setter` to the models just as you have the entity. – Matt U Jan 30 '22 at 14:44
  • @MattU, any idea on error ? – Oxana Grey Jan 30 '22 at 14:45
  • I’m not familiar with MapStruct, but the documentation at https://mapstruct.org/documentation/stable/reference/html/ emphasises that the property names follow Java standard - ie start with a LOWER case letter (they explicitly say “ e.g. seatCount for a property with the accessor methods getSeatCount() and setSeatCount().” - so try `source = id` etc. – racraman Jan 30 '22 at 15:14
  • Also offhand, I believe that `@Data` effectively includes `@Getter @Setter`, so don’t need to have those https://projectlombok.org/features/Data – racraman Jan 30 '22 at 15:17
  • does the db actually have the fields "remark" and "Id" (with capital i)? – dcolazin Jan 30 '22 at 15:25
  • @dcolazin, yes it has – Oxana Grey Jan 30 '22 at 15:29
  • Does this answer your question? [MapStruct and Lombok not working together](https://stackoverflow.com/questions/47676369/mapstruct-and-lombok-not-working-together) – Matt U Jan 30 '22 at 16:28

2 Answers2

1

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.

Filip
  • 19,269
  • 7
  • 51
  • 60
  • but MapStruct calls getter and setters, and get the private field values. And I do have Id field in existence. Its literally stuck ..! – Oxana Grey Jan 30 '22 at 17:33
  • I don't understand your comment. MapStruct does not care about the fields. It cares about the getters and setters only. Lowercase the capital "I" and everything will work – Filip Jan 30 '22 at 21:24
0

I know i answered this late and my answer is wrong for this problem, unfortunately this question shows up when searching for issue caused when using lombok and mapstruct (ie, when you are not writing your own getters and you are not mixing C# style and K&R style variable names the solution i found here is to re-order the annotation processors to start with lombok then mapstruct

Comfort Chauke
  • 126
  • 1
  • 9