0

I searched for this famous "Validation Error: Value is not valid" a lot. I know it may be related to equals method. But couldn't find what is wrong with mine.

xhtml:

<h:selectOneMenu id="ayarMenuSA" rendered="#{teklifIslemleriBean.selectedITip == 1}" converter="metalConverter" value="#{teklifIslemleriBean.satisTeklif.metal}">
      <f:selectItems value="#{teklifIslemleriBean.ayarMenu}" />
</h:selectOneMenu>

backing bean:

List<Metal> metalList = teklifIslemServisi.metalKodunaGoreMetalGetir(null, selectedMK);           
ayarMenu.clear();
for (Metal m : metalList) {
    ayarMenu.add(new SelectItem(m, "Stok No:" + m.getMetalNo() + " ,Ayar: " + m.getAyar() + " ,Ağırlık: " + m.getAgirlik() + " ,MetalKod: " + m.getMetalKod().getMetalKodu()));
}

pojo:

public class Metal implements java.io.Serializable {

    private Integer metalNo;
    private MetalTip metalTip;
    private MetalKod metalKod;
    private BigDecimal agirlik;
    private BigDecimal ayar;
    private boolean durum;

    public Metal() {
    }

    public Metal(boolean durum) {
        this.durum = durum;
    }

    public Metal(MetalTip metalTip, MetalKod metalKod, BigDecimal agirlik, BigDecimal ayar, boolean durum) {
        this.metalTip = metalTip;
        this.metalKod = metalKod;
        this.agirlik = agirlik;
        this.ayar = ayar;
        this.durum = durum;
    }


    public Integer getMetalNo() {
        return this.metalNo;
    }

    public void setMetalNo(Integer metalNo) {
        this.metalNo = metalNo;
    }

    public MetalTip getMetalTip() {
        return this.metalTip;
    }

    public void setMetalTip(MetalTip metalTip) {
        this.metalTip = metalTip;
    }

    public MetalKod getMetalKod() {
        return this.metalKod;
    }

    public void setMetalKod(MetalKod metalKod) {
        this.metalKod = metalKod;
    }

    public BigDecimal getAgirlik() {
        return this.agirlik;
    }

    public void setAgirlik(BigDecimal agirlik) {
        this.agirlik = agirlik;
    }

    public BigDecimal getAyar() {
        return this.ayar;
    }

    public void setAyar(BigDecimal ayar) {
        this.ayar = ayar;
    }

    public boolean isDurum() {
        return this.durum;
    }

    public void setDurum(boolean durum) {
        this.durum = durum;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Metal other = (Metal) obj;
        if (this.metalNo != other.metalNo && (this.metalNo == null || !this.metalNo.equals(other.metalNo))) {
            return false;
        }
        if (this.metalTip.getMetalTipiId() != other.metalTip.getMetalTipiId() && (this.metalTip == null || this.metalTip.getMetalTipiId() != (other.metalTip.getMetalTipiId()))) {
            return false;
        }
        if (!this.metalKod.getMetalKodu().equals(other.metalKod.getMetalKodu()) && (this.metalKod == null || !this.metalKod.getMetalKodu().equals(other.metalKod.getMetalKodu()))) {
            return false;
        }
        if (this.agirlik != other.agirlik && (this.agirlik == null || !this.agirlik.equals(other.agirlik))) {
            return false;
        }
        if (this.ayar != other.ayar && (this.ayar == null || !this.ayar.equals(other.ayar))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 17 * hash + (this.metalNo != null ? this.metalNo.hashCode() : 0);
        hash = 17 * hash + (this.metalTip != null ? this.metalTip.hashCode() : 0);
        hash = 17 * hash + (this.metalKod != null ? this.metalKod.hashCode() : 0);
        hash = 17 * hash + (this.agirlik != null ? this.agirlik.hashCode() : 0);
        hash = 17 * hash + (this.ayar != null ? this.ayar.hashCode() : 0);
        return hash;
    }



}

converter:

@FacesConverter("metalConverter")
public class MetalConverter implements Converter, Serializable {

    private static final long serialVersionUID = -1L;


    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        System.out.println("metalConverter getAsObject("+ str);
        Metal metal = new Metal();
        String values[] = str.split(":");

        if (values.length > 1) {
            metal.setMetalNo(Integer.parseInt(values[0]));
            metal.setAyar(new BigDecimal(values[1]));
            metal.setAgirlik(new BigDecimal(values[2]));//?? intcompact=1500
            metal.setMetalKod(new MetalKod(values[3]));
            metal.setMetalTip(new MetalTip(values[4]));
        }
        return metal;
    }

    Integer getKey(String value) {
        Integer key;
        key = Integer.valueOf(value);
        return key;
    }

    String appendString(Integer value) {
        StringBuilder sb = new StringBuilder();
        sb.append(value);
        return sb.toString();
    }

    @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {

            if (object == null) {
                return null;
            }
            if (object instanceof Metal) {
                Metal metal = (Metal) object;
                StringBuilder sb = new StringBuilder();
                sb.append(metal.getMetalNo());
                sb.append(':');
                sb.append(metal.getAyar().toString());
                sb.append(':');
                sb.append(metal.getAgirlik().toString());
                sb.append(':');
                sb.append(metal.getMetalKod().getMetalKodu());
                sb.append(':');
                sb.append(metal.getMetalTip().getMetalTipiId());
                System.out.println("metalConverter getAsString(object) -> " + sb.toString());
                return sb.toString();

            } else {
                throw new IllegalArgumentException(object + " nesnesi " + object.getClass().getName() + "tipinde geliyor; olması gereken tip ise: " + Metal.class.getName());
            }
        }

Actually if I write equals method this way:

 @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Metal other = (Metal) obj;
        return true;
    }

it works fine. But for some reasons I have to use the above code. Does anyone have any idea about this issue?

yatskevich
  • 2,085
  • 16
  • 25
lamostreta
  • 2,359
  • 7
  • 44
  • 61

1 Answers1

0

You shouldn't compare objects on equality using == or !=. You should use Object#equals() method for this. Integer for example is an object while int is a primitive and can safely be compared using ==.

You need to rewrite the equals() method as follows:

private Integer metalNo;
private MetalTip metalTip;
private MetalKod metalKod;
private BigDecimal agirlik;
private BigDecimal ayar;
private boolean durum;

// ...

public boolean equals(Object object) {
    // Basic checks.
    if (object == this) return true;
    if (object == null || getClass() != object.getClass()) return false;

    // Property checks.
    Metal other = (Metal) object;
    if (metalNo == null ? other.metalNo != null : !metalNo.equals(other.metalNo)) return false;
    if (metalTip == null ? other.metalTip != null : !metalTip.equals(other.metalTip)) return false;
    if (metalKod == null ? other.metalKod != null : !metalKod.equals(other.metalKod)) return false;
    if (agirlik == null ? other.agirlik != null : !agirlik.equals(other.agirlik)) return false;
    if (ayar == null ? other.ayar != null : !ayar.equals(other.ayar)) return false;
    if (durum != other.durum) return false;

    // All passed.
    return true;
}

If necessary, you can always let your IDE autogenerate one.

enter image description here

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • interesting because I used Netbeans insert code option to generate this method. I will implement your answer and let you know the result. Thanks for the quick response. – lamostreta Feb 15 '12 at 15:37
  • Still have the same validation error. There might be some other problem with this code, but I can not find it. – lamostreta Feb 16 '12 at 07:50
  • Maybe the `equals()` of `MetalTip` or `MetalKod` is broken. – BalusC Feb 16 '12 at 11:43