我有这样的JPA课程

@Data
@Entity
@Table(name = "oss_org_discount")
@IdClass(OrganizationDiscountPK.class)
public class OrganizationDiscount {
    @Id
    @OneToOne(fetch = FetchType.LAZY)
    @EqualsAndHashCode.Exclude
    @JoinColumn(name = "Org")
    @ToString.Exclude
    private Organization organization;

    @EqualsAndHashCode.Include
    @ToString.Include
    private String getOrganizationId() {

        return organization.getId();
    }

    @Id
    @Column(name = "Discount")
    private String discountId;

    private String active;

    @Contract(pure = true)
    public boolean isActive() {

        return "Y".equals(active);
    }

    public void setActive(final boolean active) {

        this.active = active ? "Y":"";
    }
}



还有一个IdClass。

@Data
public class OrganizationDiscountPK implements Serializable {
    private Organization organization;
    private String discountId;
}


当我查看日志时,会看到以下内容。

Hibernate: alter table oss_org_discount add constraint UK_ecc64ehkc31t6kf0hcdwj1tvi unique (org)


我的问题是,它不应该显示类似

Hibernate: alter table oss_org_discount add constraint UK_ecc64ehkc31t6kf0hcdwj1tvi unique (org, discount)


不知道这是我的错误还是Hibernate中的错误。

我确实看到了...

Hibernate: create table oss_org_discount (discount varchar(255) not null, active varchar(255), org varchar(255) not null, primary key (discount, org))


因此,另一个问题是为什么要创建唯一约束?

最佳答案

问题是OneToOne上的Organization。应该是@ManyToOne

07-23 10:36