问题描述
我有一个mysql表"countries",如下
i have a mysql table "countries" as follow
| id | name | name2 | code | area | currency | language | population |
----------------------------------------------------------------------
| .. | .. | ... | ...| ....| ... | ... | ... |
此实体"customCountry":
this entity "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,但是我不明白为什么,但是我认为我很好地管理了属性和列之间的映射.谢谢
the problem is that when i called this service method in my controller , i find out that the size = 0 , but i don't see why , i think that i well managed the mapping between attributes and columns though .thank you
推荐答案
您可以从存储库类中删除 List<customCountry> findAll();
.
You can remove List<customCountry> findAll();
from repository class.
由于您已经扩展了 JpaRepository<customCountry,Long>
,因此具有此方法.
As you had already extended JpaRepository<customCountry,Long>
, it's have this method.
当您在存储库中提供该方法而不是其实现时.这引起了一个问题.
As you are providing that method in repository but not implementation of it.That is causing an issue.
只需尝试将其删除.
这篇关于为什么实体无法映射列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!