开发者

Using collection.clear() on @ManyToMany in Spring->JPA->Hibernate results in a N delete statements

开发者 https://www.devze.com 2023-03-07 19:01 出处:网络
When using c.clear() where c is of type Collection representing a @ManyToMany owning relationship, Spring->JPA->Hibernate produces a single delete statement for every entry in the collection such as:

When using c.clear() where c is of type Collection representing a @ManyToMany owning relationship, Spring->JPA->Hibernate produces a single delete statement for every entry in the collection such as:

Hibernate: delete from printer_feature where printers_id=? and features_id=? Hibernate: delete from printer_feature where printers_id=? and features_id=? Hibernate: delete from printer_feature where printers_id=? and features_id=? Hibernate: delete from printer_feature where printers_id=? and features_id=? Hibernate: delete from printer_feature where printers_id=? and features_id=?

I read that there is something in Hibernate called batching, but not sure if it can help, or can be somehow "hinted" in the JPA annotations. I am trying to reduce the number of d开发者_StackOverflow社区elete statements here.

How can this be optimized to:

1) delete from printer_feature where printer_id = ?, after all, I am doing a clear() on the collection so it should clear everything, right?

2) delete from printer_feature where printer_id = ? and feature_id in (?, ?, ?, ....) as a compromise can this be changed to a single delete in this format?

Thanks. -AP_


I don't know of such an option. The problem with a batch delete here is that the entities you are deleting might as well have associations.

You can use an HQL query for that, but it will disregard any association cascades (the hql delete query is pretty much the same as the sql equivalent)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号