Why is my EmbeddedId in hibernate not working?

brainydexter picture brainydexter · Mar 29, 2012 · Viewed 28.5k times · Source

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
}

Answer

Kiran picture Kiran · Nov 24, 2012

You shouldn't have the @Id in the EmbeddedId Class. Remove the Id annotation in your HolidayPackageVariantPrimaryKey and it should work fine.