'No property named "Id" exists in source parameter(s). Did you mean "null"?
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 ?
Solution 1:[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.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Filip |
Solution 2 | Comfort Chauke |