Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate mapping a second @Embeddable field in a subclass

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

like image 431
Dougnukem Avatar asked Jun 04 '09 15:06

Dougnukem


2 Answers

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?

like image 66
Vincent Ramdhanie Avatar answered Sep 18 '22 00:09

Vincent Ramdhanie


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

like image 39
Bryant Avatar answered Sep 22 '22 00:09

Bryant