projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.81k stars 2.37k forks source link

[FEATURE] @FieldNameConstants Easier overwrite for generated fields #3685

Open e-hucke opened 2 months ago

e-hucke commented 2 months ago

Problem is that I have I class used for Spring Data MongoDB with a field called "id". @FieldNameConstants will generate ExampleClass.Fields.id = "id" but I need it to be "_id"

The current workaround to overwrite this field would be:

// ...
@FieldNameConstants
public class ExampleClass {

    @NoArgsConstructor(access = AccessLevel.PRIVATE) // SonarLint will complain about missing default constructor
    public static final class Fields {
        public static final String id = "_id";
    }

    private String id;

}

This solution is pretty ugly/complex and kind of defeats the point of easy to use annotations.

What I would like to see implemented:

// ...
@FieldNameConstants
public class ExampleClass {

    @FieldNameConstant(value = "_id")
    private String id;

}

FieldNameConstants is super useful for writing db queries but using it with fields that get serialized differently into the db is cumbersome since you either have to hardcode the strings into the query or use the workaround above

kaaax0815 commented 2 months ago

@Rawi01 I also encounter this problem. And it's very critical. Please consider adding this feature

Vislesha commented 2 months ago

I also came across a similar requirement and suggested a possible approach here: #3477. Would be very useful if it's implemented in @FieldNameConstants