I'm using Hibernate 4.3.2.Final and in the mapping.xml, I have a composite id like this:
<class name="OrganisationSpecialist" table="organisation_specialist">
<composite-id mapped="false" unsaved-value="undefined">
<key-many-to-one class="Organisation" column="ORGANISATION_ID" lazy="false" name="organisation" />
<key-many-to-one class="Specialist" column="SPECIALIST_ID" lazy="false" name="specialist" />
</composite-id>
</class>
The following code works
Criteria criteria = session.createCriteria(OrganisationSpecialist.class);
criteria.add(Restrictions.eq("organisation.organisationId", organisationId));
When I add an statement with a property present in my model Organisation like this:
Criteria criteria = session.createCriteria(OrganisationSpecialist.class);
criteria.add(Restrictions.eq("organisation.organisationId", organisationId));
criteria.add(Restrictions.eq("organisation.deleted", true));
I get the error message:
org.hibernate.QueryException: could not resolve property: organisation.deleted of: ch.xxx.yyy.model.OrganisationSpecialist
I also tried using alias for the property deleted like this:
Criteria criteria = session.createCriteria(OrganisationSpecialist.class);
criteria.add(Restrictions.eq("organisation.organisationId", organisationId));
criteria.createAlias("organisation", "o").add(Restrictions.eq("o.deleted", false));
criteria.add(Restrictions.eq("organisation.deleted", true));
When I'm using HQL Query, my query works.
Query query = session.createQuery("from OrganisationSpecialist where organisation.organisationId=:orgId and organisation.deleted=false");
query.setParameter("orgId", organisationId);
Can somebody explain me what I'm doing wrong? I thought that every query possible in HQL are also possible using Criteria.
Thanks for explanation