Generating normal and chainable Setters with Lombok
Hi, I am using Lombok to generate constructors, getters and Setters for my spring data model.
I think I heard that I'd need to implement getters and Setters following the bean conventions for jpa to work.
But I would like to use chainable Setters for the bussiness logic.
Do you know of any way to do this.
If there is no obvious way to do that, I would be willing to implement custom annotation processing to generate them myself.
4 Replies
⌛
This post has been reserved for your question.
Hey @NeoCortex97! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
There are multiple possibilities
One would be to just make the getters/setters return the
this
instance for chaining - this may or may not work
depending on why you need them to adhere to bean conventions and how strict that is
another way would be to use @Builder
with lombok - this way, you have have a chainable way of instantiating objects of that class while still being able to have multiple setters
that creates a new class
yet another way is two create two setters for each attribute with different names
e.g. for an attribute yourAttribute
, you have a method YOUR_CLASS yourAttribute(ATTR_TYPE yourAttribute)
and void setYourAttribute(ATTR_TYPE yourAttribute)
Implementing a custom annotation processor for that is a bad idea since that doesn't allow you to change the classes themselves (though you could create another "changer" class that itself is chainable). Lombok does illegal operations in order to do that kind of stuffI would employ a real code generator like antlr to parse the sourcecode and physically inject the methods into the file that gets compiled with the annotations simply telling the library which attributes it should exclude.
I thought generating source code would be simpler, coming from a large python project, that basically did something similar and where annotations are first class sitizens. But python being an effectively untyped langage made it considerably simpler.
I'll try doing what I thought it should work, providingmultiple annotations....
My project is a spring boot application with
spring-data-jpa
and hibernate as a persistence provider.
I could simply ignore the requirement for beans specification that jpa imposes, but I'd lose the ability to switch persistence providers easily.
Given that hibernate is not going anywhere in the forseable future, I think that could be safe.
My boss would be cool, but I personally would like to keepmy code as portable as possible.
and as boilerplate free as possible.
Having the @Accessors
annotation from lombok be repeatable would be the best solution, but I think that is not possible for now
I could otherwise work around jpas thing, but I ques havin Lombok generate a builder, or writing some builders by hand is the cleanest solution for now...Post Closed
This post has been closed by <@284061518969176064>.