小编典典

为什么我的Hibernate中的EmbeddedId不起作用?

hibernate

我有一个复合主键(IDHOLIDAYPACKAGE, IDHOLIDAYPACKAGEVARIANT)的表HolidayPackageVariant,其中IDHOLIDAYPACKAGE指的是实体
HolidayPackage许多一个 关系 HolidayPackageVariant
HolidayPackage

当我尝试在HolidayPackageVariant中进行复杂的PK映射时,出现以下错误:

初始SessionFactory创建失败。org.hibernate.annotations.common.AssertionFailure:声明类在继承状态层次结构中未找到:org.wah.model.holidaypackage.HolidayPackageVariantPrimaryKey

有人可以告诉我我在做什么错吗?

我的POJO看起来像这样:

HolidayPackageVariant:

@Entity
@Table(name="HOLIDAYPACKAGEVARIANT")
public final class HolidayPackageVariant {

    private HolidayPackageVariantPrimaryKey idCompound;

    @EmbeddedId
    public HolidayPackageVariantPrimaryKey getIdCompound() {
        return idCompound;
    }

    // other code
}

HolidayPackageVariantPrimaryKey

@Embeddable
public final class HolidayPackageVariantPrimaryKey implements Serializable {

    private Integer idHolidayPackageVariant;
    private HolidayPackage holidayPackage;

    public HolidayPackageVariantPrimaryKey(){}

    public HolidayPackageVariantPrimaryKey(int id, HolidayPackage pkg){
        setIdHolidayPackageVariant(id);
        setHolidayPackage(pkg);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "IDHOLIDAYPACKAGEVARIANT", nullable = false)
    public Integer getIdHolidayPackageVariant() {
        return idHolidayPackageVariant;
    }

    @Id
    @ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
    @JoinColumn(name="IDHOLIDAYPACKAGE", nullable=false)
    public HolidayPackage getHolidayPackage() {
        return holidayPackage;
    }

    // equals and hashCode
}

假日套餐

public final class HolidayPackage {
    private Set<HolidayPackageVariant> holidayPackageVariants = new HashSet<HolidayPackageVariant>(0);

    @OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.ALL}, mappedBy = "idCompound.holidayPackage")
    public Set<HolidayPackageVariant> getHolidayPackageVariants() {
        return holidayPackageVariants;
    }

    // other code
}

阅读 225

收藏
2020-06-20

共1个答案

小编典典

您不应该@IdEmbeddedId课程中使用。删除Id您的注释,HolidayPackageVariantPrimaryKey它应该可以正常工作。

2020-06-20