In our hibernate project, the entities are coded using the java beans pattern. There's quite a few spots in our code where someone has forgotten a to set a mutator and we get an exception due to a NOT NULL field.
Is anyone using a builder to construct their entities or making them immutable?
I'm trying to find an effective pattern that is not in the style of the java beans pattern.
Thanks
A true requirement an entity is an existence of a unique business identity (not a surrogate id that is used for technical purposes) that makes it distinguishable from other entities. Entities can be immutable, whether we talk about COW or read-only objects.
You can tell Hibernate that a specific entity is immutable by using @Entity(mutable=false) or @Immutable annotations. Note that both are Hibernate extensions to JPA standard.
@Immutable annotation can be used on Entity. JPA Will ignore all updates made to entity. This is a Hibernate document, not a JPA document.
If you make your beans immutable then you have to use field level access and this comes with its own set of problems as discussed thoroughly here. The approach we took is to have a Builder/Factory enforcing/validating the requiredness etc rules for us.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With