I have a model, that each class extends Entity:
@MappedSuperclass
public abstract class Entity implements Serializable {
private Long id;
.
.
.
}
Now, in my model, each Entity could have an attribute:
@javax.persistence.Entity
public class Attribute extends Entity {
private Entity entity;
private String name;
private String value;
.
.
.
}
My question is: How can I map entity attribute of class Attribute ? My idea is to store this attribute as entityClass and entityId, but still I don’t know how to achieve this with JPA?
Example of concrete class that extends Entity:
@javax.persistence.Entity
public class Ball extends Entity {
private Set<Attribute> attributes;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "entity")
public Set<Attribute> getAttributes() {
return attributes;
}
public void setAttributes(Set<Attribute> attributes) {
this.attributes = attributes;
}
}
There is no easy way to do this in JPA. You could map the relationship as @Transient and add tow @Basic mappings for the id and class (perhaps using property get/set method). Then execute the query in you model code.
If you are using EclipseLink you may be able to use a @VariableOneToOne,
See,
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_variableonetoone.htm#CHDDFDGF