flow icon indicating copy to clipboard operation
flow copied to clipboard

refreshItem() fails when using Lombok-bean

Open kvitso opened this issue 3 years ago • 3 comments

Description of the bug

Following the code on https://vaadin.com/docs/latest/binding-data/data-provider (section "Updating the Displayed Data"), the refresh method does not work when used on a bean annotated with @Data from Lombok. There is no error message produced.

Expected behavior

The Grid should update its item data when refreshItem() is invoked

Minimal reproducible example

https://github.com/kvitso/vaadin-grid-demo

Start with mvn spring-boot:run

http://localhost:8080/works and http://localhost:8080/fails illustrate the bug

Versions

  • Vaadin / Flow version: 24.2.4
  • Java version: 17
  • OS version: Windows 10
  • Browser version (if applicable): N/A
  • Application Server (if applicable): Tomcat
  • IDE (if applicable): N/A

kvitso avatar Nov 27 '23 00:11 kvitso

Upon further testing after discovering this, it may be more proper to treat this as a Lombok bug than a Vaadin bug. It is the equals() or hashCode() from the @EqualsAndHashCode (or @Data) annotation with Lombok that causes this to happen, indicating that the generated equals() and hashCode() methods are not properly generated. I did a test matching hashCode() for current selected item in the grid and the item sent to refreshItem(), and those were identical. So not really sure why it is not able to refresh the grid when the grid-beans are annotated with Lombok's @Data or @EqualsAndHashcode.

kvitso avatar Nov 27 '23 00:11 kvitso

As reported in the documentation and as you noticed, the items should have a proper implementation of the hashCode() and equals() methods to provide stable and unique identifiers.

In your case, the Lombok @Data annotation implements haschode and equal using all fields in the class, so even if the hashcode() value matches, the equals will return false because one of the fields has been changed. So the behavior is expected.

You can take a look at the documentation for an example where a stable identifier field is used for hashCode() and equals() implementation, instead of using all others mutable fields.

mcollovati avatar Nov 27 '23 07:11 mcollovati

Thank you for the clarification - I will close this issue then

kvitso avatar Nov 27 '23 12:11 kvitso