Reputation: 9189
Hibernate cannot seem to find the composite foreign key conceptPk
in my JPA query. How can I get hibernate to recognize the composite foreign key?
The following JPA:
@SuppressWarnings("unchecked")
public Collection<SnomedDescription> findDescriptionForConcept(SnomedConcept conc) {
Query query = this.em.createQuery("SELECT descr FROM SnomedDescription descr WHERE descr.concept =:cid");
query.setParameter("cid", conc);
return query.getResultList();
}
Is generating the following hibernate SQL, which shows that hibernate is not recognizing the embedded key:
select snomeddesc0_.effectiveTime as effectiv1_60_,
snomeddesc0_.id as id2_60_,
snomeddesc0_.active as active3_60_,
snomeddesc0_.inSnapshot as inSnapsh4_60_,
snomeddesc0_.languageCode as language5_60_,
snomeddesc0_.moduleId as moduleId6_60_,
snomeddesc0_.Term as Term7_60_
from sct2_description snomeddesc0_
where (snomeddesc0_.effectiveTime, snomeddesc0_.id)=(?, ?)
As you can see, hibernate is trying to map descriptionPK
in the where clause, when it should be mapping description.concept.conceptPk
in the where clause. How can I fix this?
Upvotes: 0
Views: 170
Reputation: 1063
Make sure you have appropriate equals/hashcode methods implemented in all entities especially Concept and ConceptKey to fix the issue you having.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((conceptPk == null) ? 0 : conceptPk.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Concept other = (Concept) obj;
if (conceptPk == null) {
if (other.conceptPk != null) {
return false;
}
} else if (!conceptPk.equals(other.conceptPk)) {
return false;
}
return true;
}
Upvotes: 1