views:

588

answers:

3

Hi. I seem to get a ConcurrentModificationException when I have a CollectionOfElements inside an Embedabble.

If would like to have it like that, however If I change Route from Embedabble's to Entity than everything works fine. I have even tried adding @Version, but that doesn't seem to work.

Here are a snippet of my classes. 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;

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> 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;

I have generated Hashcode/equals for Coordinates and Route

A: 

I can't give you any Hibernate-specific advice - but ConcurrentModificationExceptions often mean that a collection is being modified inside its iterator, such as

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

Normally you can avoid this by explicitly creating an Iterator and calling its remove() method instead of the Collection's - but if this is internal Hibernate code you won't have that option.

Andrzej Doyle
A: 

The usage of "@CollectionOfElements" and "@Embeddable" is confusing. I assume you want Route and Coordinates to be separate tables? If so, they really shouldn't be @Embeddable. @Embeddable represents something that can be embedded into the parent table. As an example, to use composite keys you typically use an @EmbeddedId as your PK, which links to a class which is @Embeddable.

Since you mention that switching to Entity seems to fix the problem, I think you should switch Route and Coordinate to separate Entities. Then you will have a much more standard model setup which should clear your problem.

Malaxeur
+3  A: 

Check this JIRA entry.

ConcurrentModificationException when collection of embeddable contains a collection

It's a known bug in the Annotation Binder. And the issue lies in Hibernate Core which doesn't support collections in collections of embedded.

jitter
+1. Embeddable within a collection can not have any collections of its own because there's no primary key defined for those (sub) collections to link to. You'll need to make Route an entity though you can still make its lifecycle to be controlled by Kart and thus semantically it'll be no different from Embeddable outside the DAO layer.
ChssPly76
Thanks. I already did that as a workaround.
Shervin