java - 为什么实体不能映射列表?
问题描述
我有一个mysql表“国家”如下
| id | name | name2 | code | area | currency | language | population |
----------------------------------------------------------------------
| .. | .. | ... | ...| ....| ... | ... | ... |
这个实体“customCountry”:
@Entity
@Table(name="countries")
public class customCountry {
@Id
@Column(name = "id",table ="countries")
private Long id;
@Column(name = "code",table ="countries")
private Long code;
@Column(name = "language",table ="countries")
private String language;
//constructors + getters/setters
}
我有这个存储库:
@Repository
public interface customCountryRepository extends JpaRepository<customCountry,Long>{
List<customCountry> findAll();
}
这个服务类:
@Service
@ComponentScan(basePackages="repository")
public class countriesService {
@Autowired
customCountryRepository customcountry_repo;
public List<customCountry> loadcustomcountries()
{
List<customCountry> validcustomcountries = customcountry_repo.findAll();
System.out.println("size: "+validcustomcountries.size());
return validcustomcountries;
}
}
问题是当我在我的控制器中调用这个服务方法时,我发现 size = 0,但我不明白为什么,我认为我很好地管理了属性和列之间的映射。谢谢你
解决方案
您可以 List<customCountry> findAll();
从存储库类中删除。
正如你已经扩展JpaRepository<customCountry,Long>
的那样,它有这个方法。
当您在存储库中提供该方法但没有实现它时。这导致了一个问题。
只需将其删除即可。
推荐阅读
- python - 使用迭代器作为列和索引更快地将总和值附加到数据帧
- javascript - 根据位置/日期更改日期选择器中的时间
- sql - 如何在 SQLite 全文搜索(fts5)中排除字段
- git - 什么是`git restore`命令,`git restore`和`git reset`有什么区别?
- math - 图中有多少个组件?
- swift - 更改宽度常量后,Swift setGradientBackground 不起作用
- react-native - 我的项目在执行“react-native log-android”时遇到问题
- sqlite - 可以像临时“表”一样分配查询结果吗?
- python - 基于文本的 RPG 重复伤害完成
- google-apps-script - 有没有比使用谷歌应用脚本的 getValues() 函数更快地从谷歌电子表格获取值的方法?