views:

2227

answers:

2

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).

JPA @Embeddable/@Embedded throws org.hibernate.MappingException: Repeated column in mapping for entity

+1  A: 

The problem seems to be this:

 public class ContentID implements Serializable {
    @Column(name="contentID")
    private String contentPath;
}

You are making the contentPath column name to be "contentId" and that is clashing with your AttributeOverride annotation later on.

Try this:

public class ContentID implements Serializable {
    @Column(name="contentPath")
    private String contentPath;
}

UPDATE I am also wondering about this:

@Embedded
@AttributeOverrides( {
    @AttributeOverride(name="contentID", column = @Column(name="awardedItem") ),
} )
private ContentID awardedItem;

You seem to be changing the name of the contentId column here to awardedItem. Is that really necessary?

Vincent Ramdhanie
The issue is not caused by the @AttributeOverride in Achievement, but because of the @Embedded ContentID that is in Achievement's base class BaseDomainObject.The trouble is in hibernate mappings the inheritance causes the object to have 2 Embedded ContentID objects as contentID column which I thought AttributeOverride would take care of it.Even if I removed the @Column(name="contentID") I think the issue would remain.
Dougnukem
A: 

I'm using

@JoinColumn(insertable=false, updatable=false)

as a workaround.

ian