java - 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 中相同的方法,可以吗?
解决方案
您需要返回类型“Long”。
@Override
Long count();
推荐阅读
- android - 如何在android的饼图中为值/图表设置clicklistener?
- c++ - C++ setw() 函数不会为打印的最后一个值添加空格(错位的宽度/空格?)
- python - UnboundLocalError:分配前引用的局部变量“照片”
- javascript - HTML 必需标签不验证表单
- python - 如何获得数据框的分类变量和数值变量之间的关系?
- c# - 如何将我的登录 ID 分配给成员以显示他们预订的课程?
- python - POST json 的替代方案(或者可以改进 json 对象“发布”)
- javascript - 无法在 React 中访问对象的属性
- plugins - 如何获取玩家的位置并将其存储在不同的树中 [Spigot 1.15.2]
- docker - 如何使用 jenkins 管道脚本创建 docker 容器