Hibernate mapping a second @Embeddable field in a subclass

18,834

Solution 1

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?

Solution 2

Vincent is right. The attributeOverride Name field is referring to a column name when it should be a attribute/property of a class.

@AttributeOverrides( {
    @AttributeOverride(name="contentPath", column = @Column(name="awardedItem") ),
} )

Notice that the name is for the class property not the database column.

See documentation

Solution 3

I'm using

@JoinColumn(insertable=false, updatable=false)

as a workaround.

Share:
18,834
Dougnukem
Author by

Dougnukem

Worked at some game companies and social game startups, working on a new web startup currently. Personal Website

Updated on July 20, 2022

Comments

  • Dougnukem
    Dougnukem almost 2 years

    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