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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/951372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:32:27  来源:igfitidea点击:

Hibernate mapping a second @Embeddable field in a subclass

javadatabasehibernateormannotations

提问by Dougnukem

I'm trying to map an @Embeddable object in a subclass whose parent class already has a field of that @Embeddable type.

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

The hibernate Embeddable Objects documentationclaims I can use the @AttributeOverrides to override the column names of an @Embeddable object:

Hibernate Embeddable Objects 文档声称我可以使用 @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;
    ...
}

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)

org.hibernate.MappingException:实体映射中的重复列:成就列:在 org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:652) 处的 contentID(应映射为 insert="false" update="false")在 org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:674) 在 org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:670) 在 org.hibernate.mapping.PersistentClass.checks6ColumnDuplication (PersistentClass.java:674) ) 在 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)

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

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

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

JPA @Embeddable/@Embedded 抛出 org.hibernate.MappingException:实体映射中的重复列

采纳答案by Vincent Ramdhanie

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.

您将 contentPath 列名称设为“contentId”,这与您稍后的 AttributeOverride 注释发生冲突。

Try this:

试试这个:

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

UPDATEI 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?

您似乎正在将此处的 contentId 列的名称更改为 AwardedItem。那真的有必要吗?

回答by Bryant

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

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

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

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

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

See documentation

查看文档

回答by ian

I'm using

我正在使用

@JoinColumn(insertable=false, updatable=false)

as a workaround.

作为解决方法。