【问题标题】:Hibernate. Repeated column when mapping @IdClass annotated entity with composite key休眠。使用复合键映射 @IdClass 注释实体时的重复列
【发布时间】:2014-10-20 00:20:03
【问题描述】:

我遇到了 Hibernate 作为 JPA 提供程序处理复合主键的问题。 我的实体如下所示

// Entity class
@Entity
@IdClass(ExternalMatchPK.class)
@Table(name = "external_match")
public class ExternalMatch {

    @Id
    @Column(name = "place_id")
    private Integer placeId;

    @Id
    @Column(name = "external_object_id")
    private Integer externalObjectId;

    // ... Other stuff here

}


// Key class
public class ExternalMatchPK implements Serializable {
    private Integer placeId;
    private Integer externalObjectId;
}

看起来很简单,但无论我做什么,我都会不断收到以下异常(为了便于阅读,行被拆分):

org.hibernate.MappingException: 
    Repeated column in mapping for entity: ExternalMatch
    column: external_object_id (should be mapped with insert="false" update="false")

我尝试将注释放在实体类字段和关键类字段上,也可以单独放置,将所有注释从字段移动到每个类上的 getter,使用关键类作为 @Embeddable 并将其放入实体中与@EmbeddedId 一起上课。似乎没有任何效果。

这个案例看起来很简单,所以可能是我们的设置有问题,但我什至无法想象到哪里寻找问题。

非常感谢任何建议。

【问题讨论】:

    标签: java hibernate jpa primary-key composite-primary-key


    【解决方案1】:

    看来我是用这个射中了自己的脚。

    问题是我在 ExternalMatchExternalObject 之间有一个双向映射,我忘记了尝试用整数 id 替换实际实体。

    变化很大

    // Entity class
    @Entity
    @IdClass(ExternalMatchPK.class)
    @Table(name = "external_match")
    public class ExternalMatch {
    
        @Id
        @Column(name = "place_id")
        private Integer placeId;
    
        @Id
        @Column(name = "external_object_id")
        private Integer externalObjectId;
    
        // ... Other stuff here
    
    }
    
    
    // Key class
    public class ExternalMatchPK implements Serializable {
        private Integer placeId;
        private Integer externalObjectId;
    }
    
    
    // Related entity class
    @Entity
    @Table(name = "external_object")
    public class ExternalObject extends AbstractNameableEntity {
    
        @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
        @JoinColumn(name = "external_object_id", nullable = false)
        private List<ExternalMatch> matches;
    
        // ...
    }
    

    表示像这样的实际映射

    // Entity class
    @Entity
    @IdClass(ExternalMatchPK.class)
    @Table(name = "external_match")
    public class ExternalMatch {
    
        @Id
        @ManyToOne
        @JoinColumn(name = "external_object_id", referencedColumnName = "id")
        private ExternalObject externalObject;
    
        @Id
        @ManyToOne
        @JoinColumn(name = "place_id")
        private Poi place;
    
        // ... Other stuff here
    
    }
    
    
    // Key class
    public class ExternalMatchPK implements Serializable {
        private Poi place;
        private ExternalObject externalObject;
    }
    
    
    // Related entity class
    @Entity
    @Table(name = "external_object")
    public class ExternalObject extends AbstractNameableEntity {
    
    
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "externalObject")
        private List<ExternalMatch> matches;
    
        // ...
    }
    

    解决了重复映射问题,但给我们留下了双向映射产生的所有熟悉的麻烦 :)

    【讨论】:

      猜你喜欢
      • 2013-07-30
      • 1970-01-01
      • 2016-02-14
      • 2016-11-20
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多