【问题标题】:Problem object cannot be cast to another object问题对象不能转换为另一个对象
【发布时间】:2020-05-19 16:43:57
【问题描述】:

我有一种方法,可以按团队中的国家/地区对数据库中的数据进行排序。 Hibernate 在 List 中返回我的数据。 TeamsDao 我的hibernate Dao方法。 Teams 在 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)

我的联赛冠军联赛班:

@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;
}

我的团队课程:

@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;
}

}

【问题讨论】:

  • 这个类是否定义在 2 个不同的依赖项中?
  • 不,那个类只定义了一次。
  • 请检查this answer,但是,它建议在引导开发工具中禁用重启。
  • @AlexRudenko 这个解决方案对我的问题不起作用。

标签: java spring hibernate


【解决方案1】:

我建议您手动删除已编译的类,并让所有源重新编译。我想我也遇到过一两次这样的问题......可能是罕见的编译器错误。

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 2015-11-19
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 2015-09-03
    • 2023-01-08
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多