As far as I'm aware, the only practical arguments in JPA world
regarding equality/hashCode is this:
1. Should we define equality based on a POJO's nature (so, the actual
fields that comprise the meat of the POJO; let's say for a person,
name, birthdate, social security number, etc).
-or-
2. Should we define equality based on a POJO's primary key (so,
whatever fields comprise that POJO's primary key. Usually a numeric
'unid' field that is on auto-increment in the db).
Lombok supports both of these cases via the use of the 'of' or
'exclude' parameter to @EqualsAndHashCode (which you can also stick on
a @Data annotated class to set specific properties for equals and
hashCode).
The rarer alternative case involves handrolling an equals and hashCode
method, which is also supported by lombok (just do it; lombok does not
overwrite existing methods).
Your case is exceedingly rare (or I could be mistaken, of course. A
pointer at 'proof' that this is quite common would help here), and can
be fixed quite easily by one of three methods:
method A: Forego @Data, as your class isn't really a straight up no-
frills struct class. Instead, add @Getter and @Setter where needed, as
well as @ToString.
method B: Use @Data, and put in expicit equals and hashCode methods
(even if they do just call super.hashCode and super.equals).
method C: Use an extension.
Extensions aren't completely documented just yet - we're planning on
adding some videos to give people a walkthrough on how to do it,
instead of reams of documentation, which is harder to write, and for
you harder to grok, than video. Nevertheless, just grab the lombok
sources and check out the existing handlers (HandleData for example).
All you need to do is replicate that kind of class, give it the
appropriate @ProviderFor annotation, and add your classes to
lombok.jar, or have them in a separate jar that's also on the
classpath (though the installer isn't quite ready to handle that case
properly just yet, so rolling your own lombok.jar would probably be
easier for now), and voila.
The biggest issue in writing your own is the same issue we face when
writing transformers: You need to write 1 for javac, and 1 for
eclipse, and in both cases you have to use the internal AST API, which
is not documented very well.
@Data intentionally has no flexibility whatsoever. The flexibility
aspect of @Data is arranged by way of its constituent annotations: If
@Data is too much, then just pick-and-mix from @ToString,
@EqualsAndHashCode, @Getter and @Setter.
On Dec 10, 10:13 pm, Matt Higgins <
matt.higg...@gmail.com> wrote:
> This is quite a common use case in fact when using JPA, where an abstract
> entity determines identity via the equals method of a super class . Further
> more equals and hashcode are hardly meaningless and have much deeper
> implications then setting a simple field or getting a value. These methods
> are often domain specific and in many cases out side of my jpa example can
> not be simplified, so either write one per class, or abstract if possible. I
> am not asking for this to be introduced into lombok merely a suggestion for
> you to take or leave, where a touch of flexibility can go a long way. In the
> JPA case equality is very difficult to define and the community at large
> debates over this all the time. The JPA debate does not need to be continued
> here but a simple out where equals and hashcode are excluded from the class
> level annotation allows developers the ability to solve the problem on their
> terms with their business needs in mind. The obvious benefit is less code,
> no getters and setters, and only one annotation.
>
> I looked around your site and could not find anything about extensions. Is
> this a forthcoming feature ? This sounds very interesting could you point
> me to some documentation? Since you are not interested in this approach it
> would be convenient to keep it on it own.
>
> -Matt
>
> On Thu, Dec 10, 2009 at 12:36 PM, Matt Higgins <
matt.higg...@gmail.com>wrote:
>
>
>
> > The difference is I do not generate the equals and hash code methods as the
> > abstract class implements them. This annotation only produces getters and
> > setters. I know with @Data I could create equals/hashcode methods in each
> > subclass to exclude the auto generated equals/hashcode. I am also ware you
> > can have the generated method call the super class equals. In my case I
> > don't want either of these. My current use case is an abstract class has the
> > ability to determine equality/identity of subclasses. If I use @Data i would
> > be putting more boiler plate code back into each sub class to override the
> > auto generated equals / hash code or use many individual @Getter / @Setter
> > annotations.
>
> > -Matt
>
> > On Thu, Dec 10, 2009 at 11:32 AM, Reinier Zwitserloot <
reini...@gmail.com>wrote:
>
> >> Exactly what does @GettersAndSetters do differently from @Data?
>
> >> On Dec 9, 10:11 pm, MattH <
matt.higg...@gmail.com> wrote:
> >> > A have forked lombok and added a new @GettersAndSetters annotation. It
> >> > is a crude copy of HandleData but it solves my larger issue with how
> >> > we deal with equals and hash code in an abstract class. I also did not
> >> > want to stray too far from the main lombok classes for easy updates.
> >> > It would be nice to see this in the main lombok source. I would be
> >> > happy to send the code if you are interested. If this were introduced,
> >> > a small refactor of HandleData would be in order.
>
> >> > -Matt
>
> >> --
> >> You received this message because you are subscribed to the Google
> >> Groups group forhttp://
projectlombok.org/
> >>
project-lombo...@googlegroups.com<project-lombok%2Bunsubscribe@go
oglegroups.com>