简体   繁体   中英

Hibernate. Repeated column when mapping @IdClass annotated entity with composite key

I've ran into problem with composite primary key handling by Hibernate as a JPA provider. My entities look like below

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

Looks pretty simple yet no matter what I do I keep getting the following exception (lines are splitted for readability):

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

I've tried placing annotation on entity class fields and key class fields together as well as separately, moving all annotations from fields to getters on each one of the classes, using key calss as @Embeddable and putting it into the entity class with @EmbeddedId . Nothing seems to work.

This case seems trivial so maybe it's something wrong with our setup but I can't even imagine where to look for the issue.

Any advice is much appreciated.

It appears that I shot myself in the foot with this.

The issue was that I had a biderectional mapping between ExternalMatch and ExternalObject I forgot about trying to replace the actual entity with its integer id.

So changing

// 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;

    // ...
}

to reprsent actual mappings like this

// 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;

    // ...
}

resolved the repeated mapping issue yet leaving us with all the familiar troubles a biderectional mapping creates :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM