Problem to filtering with DataProvider

Hi,
I need help !

I’m tryng to filtering a grid but i don’t understand why my object it’s not equal.

Here is the code :

//GRID
ListDataProvider dataProvider = new ListDataProvider<>(gmcRepository.findAll());
gmcGrid.setDataProvider(dataProvider);

//FILTER

gmcTypeComboBox.setClearButtonVisible(true);
        gmcTypeComboBox.setItems(gmcTypeRepository.findAll());
        gmcTypeComboBox.setItemLabelGenerator(GMCType::getNom);
        gmcTypeComboBox.addValueChangeListener(event -> {
             dataProvider.clearFilters();
			  if(gmcTypeComboBox.getValue() != null) {
			**  //THIS WORKING 
            **dataProvider.addFilter(gmc -> gmc.getType().getId() == gmcTypeComboBox.getValue().getId());**
			//BUT THIS NO
			**dataProvider.addFilter(gmc -> Objects.equals(gmc.getType(),gmcTypeComboBox.getValue()));**
        }
        });
		

Hi Leo

Since the items in the comboBox are freshly loaded from the DB, these instances are not the same as the gmcTypes of the gmc-items in the grid. They may represent the same thing (they have same values), but they aren’t "The Same"™, therefore the standard object-equals doesn’t work there. If you debug to that point, you will see that your JVM gives the object in gmc.getType() a different object identifier than the object in gmcTypeComboBox.getValue()

To fix this, do one of the following:

  • use your line above, which compares only the ids, (I recommend this. whats wrong with it?)
  • override the equals(Object obj) method on the GmcType class to compare only some values instead of the whole object itself, or
  • reuse the objects that your grid-dataprovider is using for the combobox also (not always possible)
  • override the equals(Object obj) method

And remember to override hashCode() if you do this.