Hibernate似乎无法在我的JPA查询中找到复合外键conceptPk
。我如何进入休眠状态以识别复合外键?
以下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();
}
正在生成以下休眠SQL,这表明休眠无法识别嵌入式密钥:
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)=(?, ?)
如您所见,hibernate尝试在where子句中映射
descriptionPK
时,它应该在where子句中映射description.concept.conceptPk
。我怎样才能解决这个问题? 最佳答案
确保在所有实体(尤其是Concept和ConceptKey)中实现了适当的equals / hashcode方法,以解决您遇到的问题。
@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;
}