0

I have a problem: When I save my object to the DB it works fine, but when I want to retrieve it from the DB, it doesn't work. I'm using selectItemsConverter from Omnifaces 1.8.3 (I tried with 1.10 too)

AutomacaoEmail.java

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

...

@ManyToOne(fetch = FetchType.EAGER)
private ModeloEmail modeloEmail;

...

@Override
public int hashCode() {
    int hash = 7;
    hash = 31 * hash + Objects.hashCode(this.id);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final AutomacaoEmail other = (AutomacaoEmail) obj;
    if (!Objects.equals(this.id, other.id)) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return String.format("%s[id=%d]", getClass().getSimpleName(), getId());
}

loadFromDatabaseMethod()

    List<ModeloEmail> modelosTemp = modeloEmailFacade.buscarTodos();
    SelectItemsBuilder selectItemsBuilder = new SelectItemsBuilder();
    if (modelosTemp != null) {
        for (ModeloEmail modeloEmail : modelosTemp) {
            selectItemsBuilder.add(modeloEmail, modeloEmail.getNome());
        }
        modelosEmails = selectItemsBuilder.buildList();
    }

page.xhtml

<p:selectOneMenu value="#{automacaoEmailsController.automacaoEmail.modeloEmail}" converter="omnifaces.SelectItemsConverter">
    <f:selectItem noSelectionOption="true" itemLabel="Selecionar um modelo"/>
    <f:selectItems value="#{automacaoEmailsController.modelosEmails}"/>
</p:selectOneMenu>

I tried with SelectItemsIndexConverter too.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197

1 Answers1

2
if (getClass() != obj.getClass()) {
    return false;
}

This test in your equals() method may fail when obj is a JPA implementation specific proxy class, such as used by Hibernate for e.g. lazy entities. That would at least explain why it seem to "sometimes" work.

If you're indeed using Hibernate, then you'd need to replace the above test by below test:

if (Hibernate.getClass(this) != Hibernate.getClass(obj)) {
    return false;
}

Or by the below more generic (not Hibernate-dependent) test:

if (!(obj.getClass().isAssignableFrom(getClass()) && getClass().isAssignableFrom(obj.getClass()))) {
    return false;
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm using eclipselink. in my case it never works. It did not work with the suggested modification. – Danilo Magrini Oct 06 '15 at 14:56
  • Then the problem is not visible in the information provided so far. Which server? Which PF version? What if you replace `noSelectionOption="true"` by `itemValue="#{null}"`? What if you replace `` by ``? – BalusC Oct 06 '15 at 17:31
  • Im using: -Glassfish server 4.x -PF 4.0.21 I already removed this line: `` didnt work. I changed to `` .. didnt work. I tried with: `List`, `List`, `SelectItem[]` ..didnt work. no error occurs. I put a inputText next to the 'select ' and the value is displayed correctly. I created a method to recharge values ​​and update the form but didnt work. I recreated the tables in the database and also created a blank page only with the component in question. Did not work. – Danilo Magrini Oct 06 '15 at 18:30
  • Sounds like as if [a "normal" converter](http://stackoverflow.com/a/4735322) would fail too. Can you inspect if the ` – BalusC Oct 07 '15 at 06:33
  • Sorry @balusc. My mistake. I implemented the method "equals" for the parent class and the `selectOneMeu` was made ​​to a related object of this class. The entity relationship had not implemented `equals` method. I'm so sorry. It works like a charm now. – Danilo Magrini Oct 07 '15 at 12:54
  • You'd better implement it in some abstract base class shared by all entities so there's no need to repeat it over all entities. See also a.o. http://stackoverflow.com/a/17343582 – BalusC Oct 07 '15 at 12:55