首页 > 解决方案 > count() 方法之间的冲突 - CrudRepository - Spring

问题描述

我想警告你,我的英语不是很完美,但我会尽力而为。

我实际上是在实习,我的任务是创建一个网络服务。在此之前,我应该使用 Maven 并创建存储库、模型等。

现在,我遇到了一个简单 Java 类的存储库的问题。

这是我的课:

package com.XXX;

import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.sql.Timestamp;

@Entity
//Generate getters and setters
@Data
//Generate a constructor with to arguments
@NoArgsConstructor
public class Day_ {
    @Id
    private int day_id_Date;
    private Timestamp day_Date;

    public Day_(int day_id_Date, Timestamp day_Date) {
        this.day_id_Date = day_id_Date;
        this.day_Date = day_Date;
    }
}

就像我说的很简单。

现在我的存储库来了:

package com.XXX;

import com.XXX.Day_;
import org.springframework.data.repository.CrudRepository;

public interface DayRepository extends CrudRepository<Day_, Long> {
}

我实际上是从互联网上的一个例子中得到的。对他很好,但对我不适用。我收到以下错误:

    Error:(6, 8) java: types org.springframework.data.repository.Repository<com.atos.test.account.tables.Day_,java.lang.Long> and org.springframework.data.repository.CrudRepository<com.atos.test.account.tables.Day_,java.lang.Long> are incompatible; both define count(), but with unrelated return types

现在我尝试count()通过执行以下操作来覆盖该方法:

package com.XXX;

import com.XXX.Day_;
import org.springframework.data.repository.CrudRepository;

public interface DayRepository extends CrudRepository<Day_, Long> {
    @Override
    long count();
}

但我收到以下错误(几乎相同):

    Error:(6, 8) java: types org.springframework.data.repository.CrudRepository<com.atos.test.account.tables.Day_,java.lang.Long> andorg.springframework.data.repository.Repository<com.atos.test.account.tables.Day_,java.lang.Long> are incompatible; both define count(), but with unrelated return types

    Error:(9, 10) java: count() in com.atos.test.account.repository.DayRepository clashes with count() in org.springframework.data.repository.Repository return type long is not compatible with java.lang.Long

我已经查看了该CrudRepository课程,但方法count()与我尝试过的方法相同。我也研究了这个Repository类,因为CrudRepository它扩展了它,但没有方法计数。

编辑

所以我想我已经解决了这个问题:我没有将我的界面扩展到CrudRepository我将它扩展到Repository. 问题是我不知道我是否可以使用与 CrudRepository 中相同的方法,可以吗?

标签: javaspringmavenrepository

解决方案


您需要返回类型“Long”。

@Override
Long count();

推荐阅读