CollectionOfElements上的java.util.ConcurrentModificationException

当我在Embedabble中有一个CollectionOfElements时,我似乎得到一个ConcurrentModificationException。

如果想拥有它,那么如果我将Route从Embedabble改为Entity,那么一切正常。 我甚至尝试过添加@Version,但这似乎不起作用。

这是我的课程片段。 Kart.java:

@Entity public class Kart { @Id @GeneratedValue private Long id; @Column(nullable=false,length=256) @NotNull @Length(max=256) private String name; @OneToOne(cascade=CascadeType.ALL) private File file; @Version private int version; @CollectionOfElements private Set route; 

Route.java:

 @Embeddable public class Route { @Parent private Kart kart; @NotNull @Column(nullable = false, length = 256) private String name; @NotNull @Column(nullable = false) private Boolean visible = Boolean.valueOf(true); @CollectionOfElements private Set coordinates; @Version private int version; 

Coordinates.java:

 @Embeddable public class Coordinates { @NotNull private int x; @NotNull private int y; @Parent private Route route; @Version private int version; 

我为Coordinates和Route生成了Hashcode / equals

检查此JIRA条目。

ConcurrentModificationException当embeddable的集合包含集合时

这是Annotation Binder中的已知错误。 而问题在于Hibernate Core,它不支持嵌入式集合中的集合。

我不能给你任何特定于Hibernate的建议 – 但ConcurrentModificationExceptions通常意味着在其迭代器中修改了一个集合,例如

 for (String s : myStringCollection) { if (s.startsWith("XXX")) { myStringCollection.remove(s); } } 

通常你可以通过显式创建一个Iterator并调用它的 remove()方法而不是Collection的来避免这种情况 – 但是如果这是内部Hibernate代码你将没有那个选项。

使用“@CollectionOfElements”和“@Embeddable”令人困惑。 我假设您希望Route和Coordinates是单独的表? 如果是这样,他们真的不应该是@Embeddable。 @Embeddable表示可嵌入父表的内容。 例如,要使用复合键,通常使用@EmbeddedId作为PK,它链接到@Embeddable类。

既然你提到切换到实体似乎解决了问题,我认为你应该将Route和Coordinate切换到单独的实体。 然后你将有一个更标准的模型设置,应该清除你的问题。