简体   繁体   English

使用Hibernate时@Immutable和@Entity(mutable=false)有什么区别

[英]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实体仍然可以通过JPQLCriteria 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.

相关问题 java中的可变和不可变String之间有什么区别 - What is difference between mutable and immutable String in java Hibernate和JPA之间的@Entity有什么区别 - What is the difference between @Entity in Hibernate and JPA 这些声明之间有什么区别? 它与一个是可变的而另一个是不可变的有关吗? - What is the difference between these declarations ? Is it related to one being mutable and the other being immutable? 使用JPA和Hibernate时JOIN和JOIN FETCH有什么区别 - What is the difference between JOIN and JOIN FETCH when using JPA and Hibernate !false 和 != false 有什么区别 - What is difference between !false and != false 获取实体管理器的休眠会话时,“ getDelegate()”和“ unwrap()”之间有什么区别 - What is the difference between 'getDelegate()' and 'unwrap()' for getting the Hibernate session of an entity manager java中的immutable和final有什么区别? - What is the difference between immutable and final in java? JPA实体和Hibernate实体之间的区别 - Difference between JPA Entity and Hibernate Entity Hibernate中“insertable = false”和“transient”之间的区别 - Difference between “insertable=false” and “transient” in Hibernate false和Boolean.FALSE有什么区别? - What is the difference between false and Boolean.FALSE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM