I’m doing a method in a REST API where you can get the comment on a publication by its ID. The error occurs in CommentServiceImplementation as it cannot resolve method equals(long)
CommentServiceImplementation
@Override
public CommentDto getCommentById(long publicationId, long commentId) {
Publication publication = publicationRepository.findById(publicationId)
.orElseThrow(() -> new ResourceNotFoundException("Publication", "id", publicationId));
//once found the publication we find the comment
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new ResourceNotFoundException("Comment", "id", commentId));
if (comment.getPublication().getId().equals(publication.getId())) {
throw new BlogAppException(HttpStatus.BAD_REQUEST, "The comment does not belong to the publication")
}
return mapDTO(comment);
}
I first find the publication. Then I find the comment. Finally, if the ID of the comment associated with a publication is not the same as the ID of the publication then the comment doesn’t belong to said publication. If it is, I return the comment as a DTO.
As far as I’ve read the problem must be that publication.getId() returns a long and that comment.getPublication().getId() has a static type that doesn’t provide an equals(long). But can’t see where my mistake is (I’m kinda following someone’s tutorial along and they have no mistake).
Publication Class
@Entity
@AllArgsConstructor @NoArgsConstructor @ToString
@Table(name = "publications", uniqueConstraints = {@UniqueConstraint(columnNames = {"title"})})
@Getter @Setter
public class Publication {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
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 = "publication", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Comment> comments = new HashSet<>();
Comment Class
@Entity
@Table(name = "comments")
@Getter@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String email;
private String body;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "publications_id", nullable = false)
private Publication publication;
CommentDTO Class
@Getter@Setter@NoArgsConstructor
public class CommentDto {
private long id;
private String name;
private String email;
private String body;
}
CommentService Class
public interface CommentService {
public CommentDto createComment(Long publicationId, CommentDto commentDto);
public List<CommentDto> getCommentByPublicationID(long publicationId);
public CommentDto getCommentById(long publicationId, long commentId);
>Solution :
.equals() is only for comparing objects. You’re trying to compare a long, which is one of the eight primitive types which you need to use == to compare. The eight primitive types in Java are int, byte, short, long, float, double, boolean and char.
if (comment.getPublication().getId() == publication.getId()) {