Hi forum,
I'm struggling to find the answer to this question, and I hope one of the experts here will show how it's done.
I have three (simplified for the question) classes:
Code:
@Entity
class Person {
@Id
long id;
@CollectionOfElements(fetch = FetchType.LAZY)
List<PersonSkill> skills;
}
@Embeddable
class PersonSkill {
@ManyToOne
Skill skill;
short level;
}
@Entity
class Skill {
@Id
long id;
String name;
}
I have a list of Person objects that I get with this:
Code:
Criteria criteria = sessionManager.getSession().createCriteria(Person.class);
I want to add a subquery to this, so I only get persons that have, for example, a Skill "Java coding" with skill level 3.
And to make things more difficult, I'll have any number of (skill, level) pairs that need to be matched. For example, "Java coding" at 3 and "SQL" at 5.
This is easy to do with SQL, but I haven't yet figured out how to do it with Hibernate. Here's one of my attempts at it:
Code:
Criteria criteria = sessionManager.getSession().createCriteria(Person.class);
DetachedCriteria skillCriteria = DetachedCriteria.forClass(PersonSkill.class, "skill");
skillCriteria.add(Restrictions.eq("skill.id", skillId));
skillCriteria.add(Restrictions.ge("skill.level", level));
criteria = criteria.add(Subqueries.exists(skillCriteria));
This gives me a MappingException "Unknown entity: ...PersonSkill".
I hope one of you will set me on the right track.
Thank you for your time.
Bo Thorsen,
Monty Program AB.