I have a compound Primary Key (IDHOLIDAYPACKAGE, IDHOLIDAYPACKAGEVARIANT)
in table HolidayPackageVariant
where IDHOLIDAYPACKAGE
refers to entity HolidayPackage with a Many to One relationship between HolidayPackageVariant and HolidayPackage.
When I try to do the compund PK mapping in HolidayPackageVariant, I get the following error:
Initial SessionFactory creation failed.org.hibernate.annotations.common.AssertionFailure: Declaring class is not found in the inheritance state hierarchy: org.wah.model.holidaypackage.HolidayPackageVariantPrimaryKey
Can someone please tell me what am I doing wrong here ?
MY POJOs look like this:
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 }
You shouldn't have the @Id
in the EmbeddedId
Class. Remove the Id
annotation in your HolidayPackageVariantPrimaryKey
and it should work fine.
I've fought once with @EmbeddedId
, and I've finished achieving the same goal with @IdClass
. The difference is, when you use @IdClass
, you don't use it in class definition, but you redeclare the same fields (however, I have then direct getters/setters for id fields, which is more comfortable for me).
Here is my example, from project I use to process freely available address database from Polish government institution GUS:
The composite key:
@Embeddable class GusPowiatPK implements Serializable { private static final long serialVersionUID = 1L; private Short powiatNr; private GusWojewodztwo wojewodztwo; @Column(name = "POW_NR") public Short getPowiatNr() { return powiatNr; } public void setPowiatNr(Short powiatNr) { this.powiatNr = powiatNr; } @ManyToOne @JoinColumn(name = "WOJ_ID") public GusWojewodztwo getWojewodztwo() { return wojewodztwo; } public void setWojewodztwo(GusWojewodztwo wojewodztwo) { this.wojewodztwo = wojewodztwo; } }
The class using it (county):
@Entity @Table(name = "POWIAT") @IdClass(GusPowiatPK.class) public class GusPowiat { private Short powiatNr; private GusWojewodztwo wojewodztwo; private String nazwa; private Date stanNa; private boolean powiatMiejski; public GusPowiat() { super(); } public GusPowiat(Short powiatNr, GusWojewodztwo wojewodztwo) { super(); this.powiatNr = powiatNr; this.wojewodztwo = wojewodztwo; } @Id public Short getPowiatNr() { return powiatNr; } public void setPowiatNr(Short powiatNr) { this.powiatNr = powiatNr; } @Id public GusWojewodztwo getWojewodztwo() { return wojewodztwo; } public void setWojewodztwo(GusWojewodztwo wojewodztwo) { this.wojewodztwo = wojewodztwo; } @Column(name = "NAZWA", length = 50, nullable = false) public String getNazwa() { return nazwa; } public void setNazwa(String nazwa) { this.nazwa = nazwa; } @Temporal(TemporalType.DATE) @Column(name = "STAN_NA", nullable = false) public Date getStanNa() { return stanNa; } public void setStanNa(Date stanNa) { this.stanNa = stanNa; } @Column(name = "POW_MIEJSKI") public boolean isPowiatMiejski() { return powiatMiejski; } public void setPowiatMiejski(boolean powiatMiejski) { this.powiatMiejski = powiatMiejski; } }
The class composing composite key (province):
@Entity @Table(name = "WOJEWODZTWO") public class GusWojewodztwo { private Short id; private String nazwa; private Date stanNa; public GusWojewodztwo() { super(); } public GusWojewodztwo(Short id) { super(); this.id = id; } @Id @Column(name = "WOJ_ID") public Short getId() { return id; } public void setId(Short id) { this.id = id; } @Column(name = "NAZWA", length = 50, nullable = false) public String getNazwa() { return nazwa; } public void setNazwa(String nazwa) { this.nazwa = nazwa; } @Temporal(TemporalType.DATE) @Column(name = "STAN_NA", nullable = false) public Date getStanNa() { return stanNa; } public void setStanNa(Date stanNa) { this.stanNa = stanNa; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With