Hi,
i struggle with a strange issue when i use @Embeddable objects that holds a reference to an Entity.
Example:
I have a parent object, that holds a collection of Embeddables (in a Set)
@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Long id;
@ElementCollection
@CollectionTable( joinColumns = @JoinColumn( name = "parent_id", nullable = false ) )
private Set<Child> children = new HashSet<Child>();
...
}
The Embeddable holds a reference to an Entity.
@Embeddable
public class Child implements Serializable {
@ManyToOne( optional = false )
private EntityRelation entityRelation;
...
}
The reference Entity
@Entity
public class EntityRelation implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Long id;
...
}
Under some circumstances[1] EclipseLink tries to insert already existent (in the database) EntityRelation objects what ends up in duplicate key exceptions.
After a bit of research i found this in the JPA 2 spec (Section 2.5):
Quote:
An embeddable class may contain a relationship to an entity or collection of entities.
Since instances of embeddable classes themselves have no persistent identity, the relationship from the referenced entity is to the entity that contains the embeddable instance(s) and not to the embeddable itself.[17]
An embeddable class that is used as an embedded id or as a map key must not contain such a relationship.
Iam not sure, but this also implies that such a mapping can't be used in sets like in the example above?
Greetings Luca
[1] For readability i will describe the use case more detailed in a new posting.
[Updated on: Tue, 27 September 2011 09:05]
Report message to a moderator