使用Hibernate时@Immutable和@Entity(mutable=false)有什么区别
如果有的话,两者有什么区别?
应该在实体上使用其中之一还是两者?
What is the difference between the two if any?
Should one or both be used on an entity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于实体来说几乎没有区别。
@Immutable
获得优先级(也就是说,如果您有同时注释为@Immutable
和@Entity(mutable = "true")
的实体,则它是将被视为不可变)。@Immutable
也可以用于具有几乎相同语义的集合。详细信息位于此处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
can also be used on collections with pretty much the same semantics. Details are hereorg.hibernate.annotations.Entity
注释已弃用,并将在 Hibernate 的未来版本中删除。因此,您应该始终使用
@Immutable
注释如果您有 Hibernate 永远不应修改的实体。@Immutable 注解告诉 Hibernate 以只读模式加载实体,因此脏检查机制无法跟踪实体修改。
但是,@Immutable 实体仍然可以通过 JPQL 或
Criteria API
批量更新查询。为了确保 @Immutable 实体永远不会被批量更新查询修改,从 Hibernate 5.2.17 开始,您可以设置以下配置属性:
使用此属性,批量更新查询最终将抛出异常异常并且实体更新将被阻止。
The
org.hibernate.annotations.Entity
annotation is deprecated and will be removed in a future release of Hibernate.Hence, you should always use the
@Immutable
annotation if you have entities that should never be modified by Hibernate.The
@Immutable
annotation tells Hibernate to load entities in read-only mode, hence entity modifications cannot be tracked by the dirty checking mechanism.However, the
@Immutable
entities can still be updated viaJPQL
orCriteria API
bulk update queries.To make sure
@Immutable
entities are never modified for bulk update queries, from Hibernate 5.2.17 onwards, you can set the following configuration property:With this property in place, a bulk update query will end up throwing an exception and the entity update will be prevented.