[英]Problem object cannot be cast to another object
我有方法,它按 class 团队中的国家/地区对数据库中的数据进行排序。 Hibernate 在 List<> 中返回我的数据。 TeamsDao 我的 hibernate Dao 方法。 团队是在 1 个依赖项中定义的。
我的错误日志:
There was an unexpected error (type=Internal Server Error, status=500).
class com.champions.league.model.Teams cannot be cast to class com.champions.league.model.Teams (com.champions.league.model.Teams is in unnamed module of loader 'app'; com.champions.league.model.Teams is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @169e859a)
java.lang.ClassCastException: class com.champions.league.model.Teams cannot be cast to class com.champions.league.model.Teams (com.champions.league.model.Teams is in unnamed module of loader 'app'; com.champions.league.model.Teams is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @169e859a)
我的联赛冠军联赛 class:
@Controller
@RequestMapping("/teams")
public class ChampLeagueController {
@GetMapping
public String showDesignForm(Model model){
List<Teams> teamsList = TeamsDao.findAll();
Teams.Countries[] countries = Teams.Countries.values();
for(Teams.Countries country: countries){
model.addAttribute(country.toString().toLowerCase(), filterByCountry(teamsList, country));
}
model.addAttribute("ChampionsLeague", new ChampionsLeague());
return "ChampionsLeague";
}
public ArrayList<Teams> filterByCountry(List<Teams> teamsList, Teams.Countries country){
ArrayList<Teams> sortedList = new ArrayList<>();
for (Teams teams : teamsList) {
if (teams.getCountries() == country)
sortedList.add(teams);
}
return sortedList;
}
我的团队 class:
@Entity
@Table(name = "teams")
public class Teams {
@Id
private int id;
private String name;
@Enumerated(EnumType.STRING)
private Countries countries;
public enum Countries{
SPAIN, ENGLAND, GERMANY, ITALY, FRANCE, NETHERLANDS
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Countries getCountries() {
return countries;
}
public void setCountries(Countries countries) {
this.countries = countries;
}
}
我建议您手动删除已编译的类,然后重新编译所有源代码。 我想我也遇到过一两次这样的问题......可能是罕见的编译器错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.