Hibernate 5 @ManyToMany无法插入重复的密钥

时间:2019-03-14 11:29:54

标签: java sql-server hibernate many-to-many

我对多对多关系有疑问。我有一个带有Set的实体Organization:

@Entity(name = "Organisation")
@Table(name = "organisation", uniqueConstraints = {@UniqueConstraint(name = "organisation_name_udx", columnNames = {"name"})})
@EntityListeners(EntityCreateListener.class)
public class Organisation implements Serializable, PatientListEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    // Some fields are here

    @ManyToMany
    @Fetch(FetchMode.SELECT)
    @JoinTable(
        name = "billing_plan_organisation",
        joinColumns = {@JoinColumn(name = "organisation_id")},
        inverseJoinColumns = {@JoinColumn(name = "billing_plan_id")})
    private Set<BillingPlan> billingPlans = new HashSet<>();

    // Some fields are here

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Organisation that = (Organisation) o;
        return Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

这是BillingPlan实体:

@Entity
@Table(name = "billing_plan")
public class BillingPlan implements Serializable {

    @Id
    @Column(unique = true, name = "id", nullable = false)
    private String id = UUID.randomUUID().toString().toUpperCase();

    @NotNull
    @Column(unique = true, name = "plan_name", nullable = false)
    private String planName;

    // Some fields are here

    @Override
    public boolean equals(Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         BillingPlan that = (BillingPlan) o;
         return id.equals(that.id) &&
                Objects.equals(planName, that.planName);
     }

     @Override
    public int hashCode() {
         return Objects.hash(id, planName);
    }
}

这是BillingPlanOrganisation实体:

@Entity
@Table(name = "billing_plan_organisation")
public class BillingPlanOrganisation implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "billing_plan_id", nullable = false)
    private String billingPlanId;

    @Id
    @Column(name = "organisation_id", nullable = false)
    private Long organisationId;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BillingPlanOrganisation that = (BillingPlanOrganisation) o;
        return Objects.equals(billingPlanId, that.billingPlanId) &&
            Objects.equals(organisationId, that.organisationId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(billingPlanId, organisationId);
        }
}

当我创建一个新组织时,它会一直保留到数据库正常。 当我将帐单计划添加到组织中时,它仍然有效。 但是,当我向组织添加另一个计费方案时,会收到错误消息:

Violation of PRIMARY KEY constraint 'billingplanorg_id_pk'. Cannot insert duplicate key in object 'dbo.billing_plan_organisation'. The duplicate key value is (8551460d-ffc9-49a9-ae92-8aa1e6b851ec, 8175).

Hibernate似乎试图再次保存组织与第一个计费计划之间的现有连接。

P.S以前,Hibernate 3可以正常运行

1 个答案:

答案 0 :(得分:0)

您是否以正确的方式覆盖了equals,hash方法?

例如

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Organisation )) return false;
    return id != null && id.equals(((Organisation ) o).id);
}

@Override
public int hashCode() {
    return 31;
}