简体   繁体   中英

Which lombok annotation fits best on JPA entity?

I use project lombok and Hibernate together. I'm wondering which lombok annotation should be generally used on entity classes.

First candidate is @Data but it generates setters for all non-final fields. Since id field is not final I have setId() method which I don't want.

The second candidate is @Value but then id field is marked as final and that's why it has to be manually assigned by an application.

I could also use @Builder but it has the same issue as @Data.

To solve problem with @Data I can write my own private setter for id field to override generated one. But maybe there is a better way? What is the best practice in this case?

i think you're mistaken on what @Data does. @Data combines a bunch of other annotations into one. it doesn't generate code for you. it performs annotations on code you have written.

I highly recommend reading Does the project lombok data annotation create a constructor of any kind

This link actually references the appropriate lombok data page itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM