Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @Embeddable with Entity relations (Mapping behaves strange when used in a Set )
@Embeddable with Entity relations [message #729953] Tue, 27 September 2011 06:56 Go to previous message
Luca Graf is currently offline Luca Graf
Messages: 17
Registered: September 2011
Junior Member
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

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic:Multi-tenancy and @PersistenceContext
Next Topic:Composite Persistence and OSGi ?
Goto Forum:
  


Current Time: Wed May 22 18:00:04 EDT 2013

Powered by FUDForum. Page generated in 0.70306 seconds