简体   繁体   中英

Problem object cannot be cast to another object

I have method, which sorting my data from db by countries in class Teams. Hibernate return my data in List<>. TeamsDao my hibernate Dao method. Teams was defined in 1 dependency.

My error log:

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)

My League champions league 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;
}

My Teams 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;
}

}

I suggest you delete your compiled classes by hand, and let all your sources recompile. I think I ran into such a problem once or twice, too... might be rare compiler bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM