Lombok how to customise getter for Boolean object field?
文章推薦指數: 80 %
You have two options to override a Getter/Setter in your class. One is the answer from First Option response. The other option is to simply ...
Home
Public
Questions
Tags
Users
Collectives
ExploreCollectives
FindaJob
Jobs
Companies
Teams
StackOverflowforTeams
–Collaborateandshareknowledgewithaprivategroup.
CreateafreeTeam
WhatisTeams?
Teams
CreatefreeTeam
CollectivesonStackOverflow
Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.
Learnmore
Teams
Q&Aforwork
Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.
Learnmore
LombokhowtocustomisegetterforBooleanobjectfield?
AskQuestion
Asked
8years,7monthsago
Modified
1year,5monthsago
Viewed
72ktimes
71
18
OneofmyPOJOshasaBooleanobjectfieldtopermitNULLSinthedatabase(arequirement).Isitpossibletousethe@DataLombokannotationatclasslevelyetoverridethegetterfortheBooleanfield?ThedefaultitgeneratesisgetXXXmethodfortheBooleanfield.IwishtooverrideitasisXXX()?
Thanks,
Paddy
javalombok
Share
Follow
editedSep29,2020at9:12
AbimaranKugathasan
28.6k1111goldbadges7070silverbadges102102bronzebadges
askedAug9,2013at3:33
PaddyPaddy
3,11255goldbadges2828silverbadges4646bronzebadges
2
Bydefinition,overridingmeans,writingyourowngetXXXforthatprovidedbylombok.IfyouwanttoaddyourownisXXX,it'sadifferentmethod.Whydon'tyoucreateanisXXXinyourPOJOandintheimplementation,callgetXXXifrequiredorimplementitthewayyoulike?
– Jayz
Aug9,2013at4:07
Ihavedeclared@Dataatclasslevelandhavetoomanyattributesintheclass,soexcludinggetterormakingthedefaultgetterprivateforonefieldwillbetough.Otherwise,evenifIaddaisXXXmethod,thegetXXXmethodwillbestillvisibletousersoftheclassanyway-IwouldratherthenusethegetXXXmethoditself.Ihopeyouunderstandmycontext.
– Paddy
Aug9,2013at4:31
Addacomment
|
5Answers
5
Sortedby:
Resettodefault
Highestscore(default)
Datemodified(newestfirst)
Datecreated(oldestfirst)
122
It'sabitverbose,butyoucanprovideyourownisXXX,andthenuseAccessLevel.NONEtotellLomboknottogeneratethegetXXX:
@Data
publicclassOneOfPaddysPojos{
//...otherfields...
@Getter(AccessLevel.NONE)
privateBooleanXXX;
publicBooleanisXXX(){
returnXXX;
}
}
(Andhey,atleastit'snotquiteasverboseasifyouweren'tusingLomboktobeginwith!)
Share
Follow
editedJul31,2019at17:09
answeredAug9,2013at4:07
ruakhruakh
165k2424goldbadges255255silverbadges292292bronzebadges
Addacomment
|
17
IthinkifyouswitchyourfieldfromBooleanXtobooleanXthanlombokgenerateagetterisX()method.
Share
Follow
answeredMar18,2016at14:36
katekate
1,64911goldbadge1010silverbadges1010bronzebadges
1
16
True,butyouneedtohavethefieldasaBooleanifthecolumnisnullableonthedatabaselevel.Theproblemisthatwithaprimitivefield(i.e.boolean)youcan'treallytellifacertainvalueissupposedtobeaNULLorazero(0).HenceOP'squestion.
– PriiduNeemre
Apr28,2016at7:44
Addacomment
|
8
IknowthequestionisoldbutIwillleavethisforfuturereferences.
YouhavetwooptionstooverrideaGetter/Setterinyourclass.
OneistheanswerfromFirstOptionresponse
Theotheroptionistosimplydefinethegetter/settermethodandlombokwillnotautomaticallyproducethespecifiedmethod.
Itesteditmyselfanditseemstoworkfine:
@Data
@AllArgsConstructor
@NoArgsConstructor
publicclassProductResponse{
privateUUIDid;
privateStringsupplierId;
privateStringsku;
privateStringname;
privateStringbrand;
privateStringimgUrl;
privateStringdescription;
privateBigDecimalprice;
privateFloatquantity;
privateStringunit;
//@Getter(AccessLevel.NONE)//Thismeans@Datawillnotproduceagetterforthisfieldsohavetoexplicitlydefineit
privateSet
延伸文章資訊
- 1@Getter and @Setter - Project Lombok
This lets you override the behaviour of a @Getter , @Setter or @Data annotation on a class. To pu...
- 2java - not - lombok override getter - Code Examples
java - not - lombok override getter. @JsonIgnore with @Getter Annotation (3). Recently i had the ...
- 3Lombok how to customise getter for Boolean object field?
You have two options to override a Getter/Setter in your class. One is the answer from First Opti...
- 4Lombok @Getter @Setter and lazy getters examples
This lets you override the behavior of a @Getter , @Setter or @Data annotation on a class. Lobmok...
- 5[Suggestion] Override Getter/Setter annotations #1026 - GitHub
It would be nice if you would implement overriding getter/setter annotations. For example: @Gette...