-
I use Quarkus (Hibernate) with Panache also offers these functions and you could replace Lombok, you just have to set the access to public. Example with Lombok:
Another Class: Without Lombok it would look like this:
Another Class: Everything I always learned about programming "Encapsulation of objects" is thrown overboard. Is this the way Quarkus is going or should Quarkus continue to be used with Lombok? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Beta Was this translation helpful? Give feedback.
-
If you want to do true object-oriented programming and encapsulation, then I'm afraid hiding field access behind getters and setters is not it. Most likely the only valid reason we've ever used getters and setters on JPA entities is not encapsulation, it's lazy loading and dirty tracking. To handle those, Hibernate ORM needs to be able to intercept read or write access to fields, and trigger inititalization (for lazy loading) or register modification (for dirty tracking). Field access in Java cannot (easily) be intercepted through proxying / bytecode manipulation; getter/setter calls can be. But, Quarkus has this peculiarity that, in order to target GraalVM, it builds applications in a closed world: it is aware of the whole code that will ever be executed, and does not accept that new JARs get introduced after the build -- because that's rarely useful anyway, and preventing it allows optimizations. Note that "field access replacement" feature is also available in the "vanilla" Hibernate ORM bytecode enhancer, under the questionable name of "extended bytecode enhancement", but is dodgy in applications that don't have a "closed-world" guarantee, so more rarely used. Also note that you don't need Panache to benefit from this "field access replacement", it's performed in Quarkus even if you're just using Hibernate ORM without Panache.
I would personally only use Lombok if I need a feature in there that is not already addressed by Quarkus. Getter/setter generation, is, IMO, largely irrelevant for Hibernate ORM entities, as Quarkus does it already (but in a more discreet, behind-the-curtains way). It could be useful for non-entity classes, for example DTOs, if you want getters/setters there. But even then, for DTOs immutability is an option, and Java records are generally a better alternative to immutable classes with getters.
Constructor and builder generation is more appealing, so if you need those, then sure, Lombok makes sense. I'm sure Lombok has many more features that can also make sense in Quarkus. |
Beta Was this translation helpful? Give feedback.
If you want to do true object-oriented programming and encapsulation, then I'm afraid hiding field access behind getters and setters is not it.
If you wanted true encapsulation, you would not have getters and setters, but you'd have meaningful, business-related methods, e.g.
person.marry(otherPerson)
,person.giveBirth(otherParent, firstName)
, etc. Which I hope you're starting to see is not exactly practical.With getters and setters, your fields are not encapsulated, they're just accessible through an additional -- arguably annoying -- layer of ceremony. You can still get and set their value an…