私のEntityクラスには、HashMapがあります。今、私はこのマップの選択を作成して、オブジェクトを選択できるようにしようとしています。そこで、次のクラスを作成しました。
HorseConverter:
@Named
public class HorseConverter implements Converter{
@EJB
private HorseBean bean;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return bean.getHorse(Long.valueOf(value));
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if(!(value instanceof Horse)){
throw new ConverterException(new FacesMessage("Object is not a Horse"));
} else {
Horse h = (Horse) value;
return Long.toString(h.getId());
}
}
}
レースエンティティ:
public Map<Horse, Integer> getHorses() {
return horses;
}
public void setHorses(HashMap<Horse, Integer> horses) {
this.horses = horses;
}
そして私の見解:
Horse:
<h:selectOneMenu value="#{betController.horse}" converter="#{horseConverter}">
<f:selectItems value="#{raceController.selectedRace.horses}" var="h" itemLabel="#{h.nickName}" itemValue="#{h}"/>
</h:selectOneMenu>
私が得ている値はHorseのインスタンスではないようです。次のリンクを確認しました: https ://stackoverflow.com/tags/selectonemenu/infoしたがって、キーは自動的に値として使用されるようです。しかし、h.keyを書いても違いはありません。
編集:これが私のハッシュであり、HorseEntityからのコードに相当します:
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + (int) (this.id ^ (this.id >>> 32));
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Horse other = (Horse) obj;
if (this.id != other.id) {
return false;
}
return true;
}