Does someone know if it is possible to establish a backreference from within a JPA @EmbeddedId
.
So for example there is an Entity of the Form
@Entity
public class Entity1 {
@Id
@GeneratedValue
private String identifier;
private Entity1 relationToEntity1;
//Left out the getters and setters for simplicity
}
And a second entity with a complex embedded Id. One part of this second entity is a reference to its parent entity. Like so:
@Entity
public class Entity2 {
@EmbeddedId private Entity2Identifier id;
//Left out the getters and setters for simplicity.
}
@Embedabble
public class Entity2Identifier {
private String firstPartOfIdentifier;
private Entity1 parent;
//Left out the getters and setters for simplicity.
}
When I try to save such a construct via JPA (Implementation is EclipseLink) to a database I get several exceptions of the form:
Exception [EclipseLink-93] (Eclipse Persistence Services - 1.1.0.r3639-SNAPSHOT):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [ENTITY1] is not present in this descriptor.
Descriptor: RelationalDescriptor(test.Entity2 --> [DatabaseTable(ENTITY2)])
Did someone encounter such a problem and has a solution?
What you are looking for is a derived Id. If you are using JPA 2.0 then the following will work. You really do not want the whole Parent to be part of the PK, just the parent's PK.
@Entity
public class Entity1 {
@EmbeddedId
private ParentId identifier;
@OneToOne(mappedBy="relationToEntity1")
private Entity2 relationToEntity2;
//Left out the getters and setters for simplicity
}
@Entity
public class Entity2 {
@EmbeddedId private Entity2Identifier id;
//Left out the getters and setters for simplicity.
@MapsId("parentId")
@OneToOne
private Entity1 parent;
}
@Embedabble
public class Entity2Identifier {
private String firstPartOfIdentifier;
private ParentId parentId;
//Left out the getters and setters for simplicity.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With