简体   繁体   English

当父级更新时,为什么Hibernate执行多个查询来更新子级?

[英]Why does Hibernate execute multiple queries for updating children when parent is updating?

I have a many-to-one relationship with inverse="false" and cascade="all" . 我与inverse="false"cascade="all"有多对一的关系。 When I update the parent, there is an update query for this parent object and multiple update queries - one for each child object in child's collection. 当我更新父对象时,有一个针对该父对象的更新查询和多个更新查询-一个针对子对象集合中每个子对象的查询。

Could you tell me why there are these queries for children? 你能告诉我为什么对孩子有这些疑问吗? And how might I avoid them? 我该如何避免呢?

If you have cascade="all" that means that every operation on parent entity will call same operation on child entities. 如果您具有cascade =“ all”,则意味着父实体上的每个操作都将在子实体上调用相同的操作。 So you have to remove cascade=all if you want not to cascade operations on child entities from association. 因此,如果您不想从关联中级联对子实体的操作,则必须删除cascade = all。

But you should be careful with that configuration, because if you don't have cascade persist (for example) created child entity will not be persisted even it is created regularly, but you have to explicitly call save on every child entity. 但是您应该谨慎使用该配置,因为(例如)如果您没有级联持久性,则即使定期创建创建的子实体也不会持久化,但是您必须在每个子实体上显式调用save。

You can read more about cascade types on this thread. 您可以在此线程上阅读有关级联类型的更多信息

您要防止Hibernate更新子实体,应更改级联类型,删除cascade=all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM