I have a situation where a persistent object can be 'owned' by one of several classes that implement a given interface.
I have used the @Any annotation to successfully map the 'child' portion of the relationship (see class Attribute Group below).
What I cannot seem to get to work is the 'parent' side of the relationship. (see class AdhocMaterialGroup below)
When I attempt to run this, the exception at start up is:
org.hibernate.MappingException: Foreign key (FK4E0B69636E7AFB69:AttributeGroup [ownerType,ownerId])) must have same number of columns as the referenced primary key (AdhocMaterialGroup [id])
If I comment out the references in AdhocMaterialGroup to AttributeGroup, SchemaExport can run successfully.
How can I map the parent -> child side when the child maps the parent as @Any?
Environment:
Tomcat 6.0.20/Hibernate Annotations 3.4.0.GA/Hibernate 3.3.2.GA/Hibernate Commons Annotations 3.1.0.GA/Spring 2.5.6
Relevant portions of these classes :
Code:
@Entity
@Table(name = "AttributeGroup")
public class AttributeGroup extends AbstractEditableObject
{
...
@Any(metaColumn = @Column(name = "ownerType"), fetch = FetchType.LAZY)
@AnyMetaDef(idType = "string", metaType = "string", metaValues =
{
@MetaValue(value = "InventoryClass", targetEntity = InventoryClass.class),
@MetaValue(value = "InventorySubClass", targetEntity = InventorySubClass.class),
@MetaValue(value = "AdhocMaterialGroup", targetEntity = AdhocMaterialGroup.class)
}
)
@JoinColumn(name = "ownerId")
private AttributeGroupOwner owner;
...
}
@Entity
@Table(name = "AdhocMaterialGroup")
public class AdhocMaterialGroup extends AbstractEditableObject implements AttributeGroupOwner
{
...
@OneToMany(fetch = FetchType.EAGER, targetEntity = AttributeGroup.class, mappedBy = "owner")
private Set<AttributeGroup> attributeGroups = new HashSet<AttributeGroup>(0);
...
}
@MappedSuperclass
public abstract class AbstractEditableObject implements EditableObject
{ ...
@Id
@GenericGenerator(name = "guid", strategy = "guid")
@GeneratedValue(generator = "guid")
@Column(name = "id", unique = true, nullable = false, updatable = false, columnDefinition = "uniqueidentifier")
private String id;
...
}
public interface AttributeGroupOwner
{
public Set<AttributeGroup> getAttributeGroups();
public void setAttributeGroups(Set<AttributeGroup> attributeGroups);
}