I’m trying to map an @Embeddable object in a subclass whose parent class already has a field of that @Embeddable type.
The hibernate Embeddable Objects documentation claims I can use the @AttributeOverrides to override the column names of an @Embeddable object:
e.g.
@Entity
public class Person implements Serializable {
// Persistent component using defaults
Address homeAddress;
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="iso2", column = @Column(name="bornIso2") ),
@AttributeOverride(name="name", column = @Column(name="bornCountryName") )
} )
Country bornIn;
...
}
Here’s the case I have:
@Embeddable
public class ContentID implements Serializable {
@Column(name="contentID")
private String contentPath;
}
@MappedSuperclass
public abstract class BaseDomainObject implements Serializable {
@Embedded
private ContentID contentID;
}
public class Achievement extends BaseDomainObject {
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="contentID", column = @Column(name="awardedItem") ),
} )
private ContentID awardedItem;
}
The error I get is:
org.hibernate.MappingException:
Repeated column in mapping for entity:
Achievement column: contentID (should
be mapped with insert=”false”
update=”false”) at
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:652)
at
org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:674)
at
org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:670)
at
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:696)
at
org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:450)
at
org.hibernate.mapping.SingleTableSubclass.validate(SingleTableSubclass.java:43)
at
org.hibernate.cfg.Configuration.validate(Configuration.java:1108)
at
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1293)
at
org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
UPDATE:
I looked in for Hibernate issues relating to this and the GRAILS project claimed they fixed this issue but their annotation solution doesn’t seem to be valid javax.persistence annotations (maybe it’s a new version).
The problem seems to be this:
You are making the contentPath column name to be “contentId” and that is clashing with your AttributeOverride annotation later on.
Try this:
UPDATE
I am also wondering about this:
You seem to be changing the name of the contentId column here to awardedItem. Is that really necessary?