我对Hibernate框架非常陌生,并且在运行代码时遇到异常:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: LeiRecord column: hash (should be mapped with insert="false" update="false")


我在SpringConfiguration,oracleDataDao的许多地方都遇到了此错误,并且由于我没有在映射中定义重复的列,所以不了解代码在哪里出错

最佳答案

您应该将类​​Address注释为@Embeddable,然后在LeiRecord实体中为addressLegaladdressHeadquearters引用其实例。

@Embeddable
@Getter
@Setter
public class Address {

   private String firstAddr;
   private String nextAddr;
   //... etc.
}

@Getter
@Data
@Entity
@Table(name = "BK_TEST")
@Immutable
public class LeiRecord {
// ...
    @Embedded
    @AttributeOverrides(
        {
            @AttributeOverride(name = "firstAddr",   column = @Column(name = "legalAddress_firstAddr")),
            @AttributeOverride(name = "nextAddr",    column = @Column(name = "legalAddress_nextAddr")),
            @AttributeOverride(name = "region",      column = @Column(name = "legalAddress_region")),
            @AttributeOverride(name = "postalCode",  column = @Column(name = "legalAddress_postalCode")),
            @AttributeOverride(name = "city",        column = @Column(name = "legalAddress_city")),
            @AttributeOverride(name = "country",     column = @Column(name = "legalAddress_country")),
            @AttributeOverride(name = "mailRouting", column = @Column(name = "legalAddress_mailRouting"))
        }
    )
    private Address addressLegal;

    @Embedded
    @AttributeOverrides(
        {
            @AttributeOverride(name = "firstAddr",   column = @Column(name = "headquartersAddress_firstAddr")),
            @AttributeOverride(name = "nextAddr",    column = @Column(name = "headquartersAddress_nextAddr")),
            @AttributeOverride(name = "region",      column = @Column(name = "headquartersAddress_region")),
            @AttributeOverride(name = "postalCode",  column = @Column(name = "headquartersAddress_postalCode")),
            @AttributeOverride(name = "city",        column = @Column(name = "headquartersAddress_city")),
            @AttributeOverride(name = "country",     column = @Column(name = "headquartersAddress_country")),
            @AttributeOverride(name = "mailRouting", column = @Column(name = "headquartersAddr_mailRouting"))
        }
    )
    private Address addressHeadquarters;

    //... other entity fields

}

09-08 02:17