当前位置: 首页 > 知识库问答 >
问题:

JAVA-无法解析方法“.equals()”[重复]

翟缪文
2023-03-14

我正在REST API中执行一个方法,您可以通过其ID获取出版物上的注释。CommentServiceImplementation中出现错误,因为它无法解析方法等于(long)

评论服务实施

@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);
}

我首先找到了出版物。然后我找到了评论。最后,如果与发布相关联的注释ID与发布的ID不同,则该注释不属于所述发布。如果是,我会将注释作为DTO返回。据我所知,问题一定是出版物。getId()返回一个长的和注释。getPublication()。getId()的静态类型不提供等于(长)。但看不出我的错误在哪里(我有点跟着别人的教程,他们没有错)。

出版物类

@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<>();

注释类

@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;

注释到类

@Getter@Setter@NoArgsConstructor
public class CommentDto {

    private long id;
    private String name;
    private String email;
    private String body;
}

CommentService类公共接口CommentService{

public CommentDto createComment(Long publicationId, CommentDto commentDto);

public List<CommentDto> getCommentByPublicationID(long publicationId);

public CommentDto getCommentById(long publicationId, long commentId);

共有1个答案

华萧迟
2023-03-14

<代码>。equals()仅用于比较对象。您正在尝试比较长的,这是您需要使用比较的八种基本类型之一。Java中的八种基本类型是int、byte、short、long、float、double、boolean和char。

if (comment.getPublication().getId() == publication.getId()) {
 类似资料:
  • 问题内容: 如何覆盖对象类中的equals方法? 即我有 我想将参数obj转换为Person类型,但是如果执行(Person)obj,它将无法正常工作。 问题答案: 您可以将其强制转换为方法,只需使用的实例确保其类型正确

  • 我试图使用for循环迭代一系列的日期,我打算使用plusDays增加开始日期,但是我得到了“Canland resolve method plusDays in date”。下面是我的代码:

  • 我是Scala的新手。我正在编写以下代码,其中一个APIendpoint在文件中缓存值(某某JsonData.toString()),另一个endpoint从该文件中检索(Json.parse())。使用wh-thenEnter编写测试时,会出现重载方法错误。 我哪里做错了? 缓存文件内容: 缓存ontroller.scala CacheControlllerTest.scala

  • 我试图理解方法引用在java中是如何工作的。乍一看,这很简单。但当涉及到这些事情时: Foo类中有一个方法: 在另一个类Bar中有这样一个方法: 并使用方法参考: 它符合并工作,但我不明白它如何匹配这个: 到BiFunction方法: ???

  • 本文向大家介绍java中重写equals()方法的同时要重写hashcode()方法(详解),包括了java中重写equals()方法的同时要重写hashcode()方法(详解)的使用技巧和注意事项,需要的朋友参考一下 object对象中的 public boolean equals(Object obj),对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 tr

  • 我正在通过Android Studio中的一个应用程序工作,该应用程序使用学校意图传递数据。我已经创建了传递数据的对象,并启动了,但是我不断收到一个警告,说我的方法无法解析。有什么想法吗?提前谢了。