我在表(IDHOLIDAYPACKAGE, IDHOLIDAYPACKAGEVARIANT)
中有一个复合主键HolidayPackageVariant
,其中IDHOLIDAYPACKAGE
引用的实体HolidayPackage具有。许多至:HolidayPackageVariant和HolidayPackage之间的一种关系。
当我尝试在HolidayPackageVariant中进行复杂的PK映射时,出现以下错误:
有人可以告诉我我在做什么错吗?
我的POJO看起来像这样:
@Entity
@Table(name="HOLIDAYPACKAGEVARIANT")
public final class HolidayPackageVariant {
private HolidayPackageVariantPrimaryKey idCompound;
@EmbeddedId
public HolidayPackageVariantPrimaryKey getIdCompound() {
return idCompound;
}
// other code
}
@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
}
最佳答案
您不应在@Id
类中包含EmbeddedId
。删除您的Id
中的HolidayPackageVariantPrimaryKey
批注,它应该可以正常工作。