Hi, I have a CityListPage, where City objects are listed in a DataView. The City has a many to one relationship to Country (eager fetching).
For each item in the list there's a edit link to CityEditPage. When the user clicks the link, the city object is passed to the CityEditPage. There's a DropDownChoice for selecting the Country. This is filled with a list of Countries from the DB. As default selection that Country must be shown, which is connected to the actual city object. It is in the list, but the selection box shows : "Choose one" Reason is, that the Country class contains an equal() method. Wicket calls this method, but the comparison of the id delivers "false" (different instances of the Integer object). I removed the id comparison, but now the String.equals() method for the name property delivers false, although the characters in the name string are identical. Should I pass the DetachableCityModel instead of the city object like discussed in http://markmail.org/message/3na4sq765uifvw3i Would this change something in the way of the comparison ? Thanks in advance Andrea ---- public class CityListPage extends SecuredBasePage { @SpringBean private CityService cityService; ... private class CitySearchResultDataView extends DataView { public CitySearchResultDataView(String id, IDataProvider dataProvider) { super(id, dataProvider); } protected void populateItem(final Item item) { final City city = (City) item.getModelObject(); //Edit Link PageLink editLink = new PageLink("editCity", new IPageLink() { public Page getPage() { return new CityEditPage(city, CityListPage.this); <--- } public Class getPageIdentity() { return CityEditPage.class; } }); item.add(new Label("id", String.valueOf(city.getId()))); item.add(new Label("name", city.getName())); item.add(new Label("country", city.getCountry().getCode())); item.add(editLink); } } private abstract class CitiesDataProvider implements IDataProvider { ... public IModel model(Object object) { return new DetachableCityModel((City) object); } } class CitiesByExampleDataProvider extends CitiesDataProvider { ... protected List<City> load() { if (exampleCity == null) return Collections.emptyList(); return getCityService().loadByExample(exampleCity); } ... } @SuppressWarnings("serial") private class DetachableCityModel extends LoadableDetachableModel { private Integer id; public DetachableCityModel(Integer id) { this.id = id; } public DetachableCityModel(City city) { super(city); this.id = city.getId(); } protected Object load() { return (id != null)? getCityService().loadById(id) : null; } private CityService getCityService() { return CityListPage.this.cityService; } } } --------------- public CityEditPage(City city, Page source) { private Page sourcePage; private City city; public CityEditPage(City city, Page source) { if(city == null) { isNew = true; city = new City(); } this.city = city; this.sourcePage = source; CityEditForm cityEditForm = new CityEditForm("cityEditForm", city); add(cityEditForm); } private final class CityEditForm extends Form { public CityEditForm(String id, City city) { super(id); add(new RequiredTextField("name", new PropertyModel(city, "name"))); add(new RequiredTextField("code", new PropertyModel(city, "code"))); add(new CountryDropDownChoice("countrySelect", city)); <--- default value not found ... private final class CountryDropDownChoice extends DropDownChoice { @SpringBean CountryService countryService; public CountryDropDownChoice(String id, Object object) { super(id); setModel(new PropertyModel(object, "country")); setChoices(countryService.loadAll()); setChoiceRenderer(new CountryChoiceRenderer()); } } } } ------- public class Country implements Serializable { private Integer id; private String name; ... @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Country other = (Country) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) <-- false return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) <-- false return false; ... } ... }
