Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to have immutable JPA entities?

Tags:

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

like image 966
Billworth Vandory Avatar asked Dec 30 '10 05:12

Billworth Vandory


People also ask

Can entity immutable?

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.

Can we create immutable class in Hibernate?

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.

What is @immutable in JPA?

@Immutable annotation can be used on Entity. JPA Will ignore all updates made to entity. This is a Hibernate document, not a JPA document.


1 Answers

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.

like image 68
Aravind Yarram Avatar answered Oct 03 '22 03:10

Aravind Yarram