[FEATURE] @EqualsAndHashCode for records
Describe the feature
Yes, I know - equals() and hashCode() are automatically generated for records, but there's no easy way to customise them. Sometimes you don't want to compare all object properties, e.g. in case of DB entities it's usually enough to implement equals/hashCode using only the ID. In case of classes, it's very easy to customise it with Lombok:
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class MyEntity {
@Id
@EqualsAndHashCode.Include
private UUID id;
private String name;
}Unfortunately, I can't do the same with records - they are not supported by Lombok's @EqualsAndHashCode annotation. This piece of code fails to compile with java: @EqualsAndHashCode is only supported on a class. error:
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public record MyEntity (
@Id
@EqualsAndHashCode.Include
UUID id,
String name
) {}I think it would be great to have this annotation also for records, and throw the compilation error only if @EqualsAndHashCode.Include/Exclude is not used in the code.
Describe the target audience
Anyone who migrates POJO classes to Java records, but has specific requirements regarding equals() and hashCode() methods.
Source: projectlombok/lombok