Search code examples
hibernatespring-data-jpaspring-dataimmutabilityspring-annotations

Spring Data JPA Immutable Entity


Using Spring Data JPA with Hibernate, are these two annotations interchangeable?

  1. org.hibernate.annotations.Immutable
  2. org.springframework.data.annotation.Immutable

More specifically, can the Spring annotation replace the Hibernate annotation, to avoid any direct Hibernate dependency in the entity? If not, is there any reason to have both annotations on the same entity?


Solution

  • No, they are not interchangeable.

    For Hibernate you need to use the Hibernate one.

    If you use Spring Data modules except Spring Data JPA, i.e. those where Spring Data does the persisting itself you need to use the Spring Data annotation.

    In both cases the other annotation will be completely ignored.