JPA update many-to-many deleting records

∥☆過路亽.° 提交于 2019-12-06 09:13:35

Use Set instead of List solved the problem. But I have no idea why it works.

Another solution provided by Hibernate is to split the @ManyToMany association into two bidirectional @OneTo@Many relationships. See Hibernate 5.2 documentation for example.

If a bidirectional @OneToMany association performs better when removing or changing the order of child elements, the @ManyToMany relationship cannot benefit from such an optimization because the foreign key side is not in control. To overcome this limitation, the link table must be directly exposed and the @ManyToMany association split into two bidirectional @OneToMany relationships.

Jherico

Its probably related to this question. You have to ensure you have an appropriately defined hashCode an equals method in your mapped object so that Eclipselink can determine equality and thus determine that the existing objects map to existing objects in the DB. Otherwise it has no choice but to recreate the child objects every time.

Alternatively, I've read that this kind of join can only support efficient adding and removing of list items if you use an index column, but that's going to be EclipseLink specific, since the JPA annotations don't seem to support such a thing. I know there is an equivalent Hibernate annotation, but I don't know what it would be in Eclipselink, if such a thing exists.

Try this one:

1) change declaration to:

private List<UserType> types = new Vector<UserType>();

2) never call

user.setTypes(newTypesList)

3) only call

user.getTypes().add(...);
user.getTypes().remove(...);

It appears my problem was that I was not merging the entity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!