gpt4 book ai didi

java - 为什么我的 EmbeddedId 在 hibernate 中不起作用?

转载 作者:IT老高 更新时间:2023-10-28 20:48:24 25 4
gpt4 key购买 nike

我在表 HolidayPackageVariant 中有一个复合主键 (IDHOLIDAYPACKAGE, IDHOLIDAYPACKAGEVARIANT),其中 IDHOLIDAYPACKAGE 指的是实体 HolidayPackage HolidayPackageVariantHolidayPackage 之间存在 ManyOne 关系。

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

Initial SessionFactory creation failed.org.hibernate.annotations.common.AssertionFailure: Declaring class is not found in the inheritance state hierarchy: 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
}

HolidayPackage

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
}

最佳答案

您不应该在 EmbeddedId 类中包含 @Id。删除 HolidayPackageVariantPrimaryKey 中的 Id 注释,它应该可以正常工作。

关于java - 为什么我的 EmbeddedId 在 hibernate 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9923611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com