Hibernate映射子类中的第二个@Embeddable字段

时间:2009-06-04 15:43:22

标签: java database hibernate orm annotations

我正在尝试在子类中映射@Embeddable对象,其父类已经具有@Embeddable类型的字段。

hibernate Embeddable Objects documentation声明我可以使用@AttributeOverrides覆盖@Embeddable对象的列名:

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;
    ...
}

以下是我的情况:

 @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;
}   

我得到的错误是:

  

org.hibernate.MappingException:   实体映射中的重复列:   成就栏:contentID(应该   用insert =“false”映射   更新=“假”)   org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:652)     在   org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:674)     在   org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:670)     在   org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:696)     在   org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:450)     在   org.hibernate.mapping.SingleTableSubclass.validate(SingleTableSubclass.java:43)     在   org.hibernate.cfg.Configuration.validate(Configuration.java:1108)     在   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1293)     在   org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)

更新

我查找了与此相关的Hibernate问题,GRAILS项目声称他们修复了这个问题,但他们的注释解决方案似乎不是有效的javax.persistence注释(也许是新版本)。

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

3 个答案:

答案 0 :(得分:7)

问题似乎是:

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

您正在将contentPath列名称设置为“contentId”,并且稍后会与您的AttributeOverride注释发生冲突。

试试这个:

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

<强>更新 我也想知道这个:

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

您似乎正在将contentId列的名称更改为awardsItem。这真的有必要吗?

答案 1 :(得分:2)

文森特是对的。 attributeOverride名称字段指的是列名称,它应该是类的属性/属性。

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

请注意,该名称用于class属性而不是数据库列。

请参阅documentation

答案 2 :(得分:1)

我正在使用

@JoinColumn(insertable=false, updatable=false)

作为解决方法。