Tricky. I'm all for doing the right thing automatically - in fact, in
the trunk, Roel, before he went on vacation, added code to 'do the
right thing' in the face of @NotNull/@NonNull annotations on fields
with @Getter/@Setter. A null check is added, and the annotation is
copied to the parameter (setters) and return type (getters). The
problem with doing the right thing automatically is that it becomes
rather difficult for folks to figure out what the heck's going on. I'm
convinced we need some sort of tool to print the result of lombok's
transformations, and preferably a nice plugin for eclipse (and the
other IDEs) that adds a toggle button in the IDE chrome.
I'm definitely not a fan of solution #2, especially considering that
if you really need equality/hashCode keyed off of just 1 field,
rolling them yourself is a lot easier. The include idea in #3 mirrors
the current exclude concept, so it's okay. Doing the right thing is
even better if we can get past the magic factor. So, let's see:
1. Is there ANY situation you can think of where just keying off of
all fields annotated with JPA @Id, if there is at least 1 field tagged
with @Id, is NOT the correct action to take? I can't really think of
any. If the "@Id Long id" field is on the class in the first place,
then any other notion of equality is right out the window - 2 objects
with the same everything wouldn't be equal if they didn't have equal
ids. So, you'd HAVE to explicitly exclude the id, either by marking it
transient, or by excluding it, which lombok can pick up on and
disregard that field for the "@Id fields win out for equals and
hashCode" rule.
2. As lombok needs to take a wild guess at typing resolutions, is
there any other annotation named 'Id' that's famous*? That would make
it more likely that lombok would do the wrong thing, which is very bad
if lombok does this silently without any feedback.
If there's absolutely no way you can possibly imagine an equals
working any other way (condition #1) and there's no other commonly
used @Id, please add an issue to key off of only fields annotated with
@
javax.persistence.Id if there's at least 1 field present with that
annotation.
What happens if you mark 2 fields with @Id, anyway? I thought that's
how compound IDs were handled. Eh, I never use compound IDs, I don't
have experience with that particular case.
*) Lombok runs before all types have been resolved. Lombok has its own
type resolver but as it doesn't know about your class or source path,
it takes a wild stab at it by scanning for other types in the same
file, and import statements. It gets confused if you use a lot of *
imports, though. I doubt you'll ever get into trouble because of this,
but if there's another common annotation also named @Id this will blow
up (and do so silently due to the 'lombok silently does the right
thing' spiel) if you use a lot of star imports.