Entities As Immutable Objects

Entities can be changed. Why represent them as immutable objects?

We get all benefits and downsides that immutable objects have.

Working with ORM frameworks, e.g., Hibernate

We create entity classes. We define relations between entity classes and many other things. Hibernate take care for a lot of things. When we have a mutable managed entity object and we set some new value to it, at commit hibernate create an update statement and execute it.

When we have a immutable entity object, we can’t set a new value to it. We have to create a new entity object. When we create a new entity object, we have to merge it with a hibernate session. Because hibernate can change entities that are managed by a hibernate session, we can’t use them as immutable objects. We have to create separate classes for immutable entity classes.

Leave a Reply