I'm working on a middleware that I have to develope for a course at university. The database was a perious task and the fortend is given. My problem is probalby about mismatching datatypes - sounds easy but it is driving me nuts because changing the frontendcode would probably the easiest way to solve it but thats not allowed unfortunately.
First off, the relevant code of the frontend:
Code:
public interface CountryDataInterface {
...
public abstract CityDataInterface getCapital();
public abstract List<CityDataInterface> getCities();
...
}
public class CountryData implements CountryDataInterface {
...
private CityDataInterface capital = null;
private List<CityDataInterface> cities = null;
...
public CityDataInterface getCapital() {
return capital;
}
public List<CityDataInterface> getCities() {
return cities;
}
public void setCapital(CityDataInterface capital) {
this.capital=capital;
}
public void setCities(List<CityDataInterface> cities) {
this.cities = cities;
}
...
}
Now, I got my mapping file for CountryData:
Code:
<class name="wfb.definitions.CountryData" table="prak228.myCountryView">
...
<many-to-one name="capital" class="wfb.definitions.CityData" unique="true" />
<set name="cities" table="prak228.myCityToCountryView">
<key column="landName" not-null="true"/>
<many-to-many class="wfb.definitions.CityData" column="name" unique="true"/>
</set>
<!-- countries and cities are linked in the view myCityToCountryView with landName referencing a country and name referencing a city -->
...
</class>
I'm not quite sure if it's correct, although it sould be according to a few examples I've worked through.
And finally my applications with the following functionality:
Code:
...
public CountryDataInterface getCountryData(String countryName) {
Query query = session.createQuery("from CountryData cd where cd.name = :cname");
query.setString("cname",countryName);
return query.list();
}
...
I'm getting:
Code:
IllegalArgumentException occurred while calling setter of wfb.definitions.CountryData.cities, caused by argument type mismatch
caused by query.list(). So, my setter setCities doesn't get an argument of the type List<CityDataInterface> but of the type List<CityData> I suppose - is there any way to check that? Although CityData implements CityDataInterface, it won't match.
But loading a country's capital is working, although in my mapping file, it's of the type CityData and not CityDataInterface.
So, why does it work with a single City but not with a list of cities and how to solve that issue?
It's the first time I'm working with hibernate therefor don't be mad about possibly stupid mistakes. I've spent hours reading documentations and searching the net, unfortunately without finding something that helps me with that issue.
I'll gratefully take any advice!