[英]What is the difference between @Immutable and @Entity(mutable=false) when using Hibernate
What is the difference between the two if any?如果有的话,两者有什么区别?
Should one or both be used on an entity?应该对一个实体使用一个还是两个?
For entity there's practically no difference.对于实体,几乎没有区别。 @Immutable
gets priority (that is if you have entity that's annotated both as @Immutable
and @Entity(mutable = "true")
it is going to be treated as immutable). @Immutable
获得优先权(也就是说,如果您的实体同时被注释为@Immutable
和@Entity(mutable = "true")
它将被视为不可变的)。
@Immutable
can also be used on collections with pretty much the same semantics. @Immutable
也可以用于具有几乎相同语义的集合。 Details are here详情在这里
The org.hibernate.annotations.Entity
annotation is deprecated and will be removed in a future release of Hibernate. org.hibernate.annotations.Entity
注释已被弃用,并将在 Hibernate 的未来版本中删除。
Hence, you should always use the @Immutabale
annotation if you have entities that should never be modified by Hibernate.因此,如果您有永远不应被 Hibernate 修改的实体,则应始终使用@Immutabale
注释。
The @Immutable
annotation tells Hibernate to load entities in read-only mode, hence entity modifications cannot be tracked by the dirty checking mechanism. @Immutable
注释告诉 Hibernate 以只读模式加载实体,因此脏检查机制无法跟踪实体修改。
However, the @Immutable
entities can still be updated via JPQL
or Criteria API
bulk update queries.但是, @Immutable
实体仍然可以通过JPQL
或Criteria API
批量更新查询进行更新。
To make sure @Immutabale
entities are never modified for bulk update queries, from Hibernate 5.2.17 onwards, you can set the following configuration property:为了确保@Immutabale
实体永远不会被修改以进行批量更新查询,从 Hibernate 5.2.17 开始,您可以设置以下配置属性:
<property
name="hibernate.query.immutable_entity_update_query_handling_mode"
value="exception"
/>
With this property in place, a bulk update query will end up throwing an exception and the entity update will be prevented.有了这个属性,批量更新查询最终会抛出异常,实体更新将被阻止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.