我再次遇到HQL问题:(
我想要的SQL是这样的:
select employee.idemployee as id, employee.age as age,
employee.birthday as birthday, employee.firstName as firstName,
employee.gender as gender, employee.lastName as lastName from employee
employee
inner join employee_skillgroups skillgroup1 on
employee.idemployee=skillgroup1.idemployee
inner join employee_skillgroups skillgroup2 on
employee.idemployee=skillgroup.idemployee
where skillgroup1.idskillgroup = 'Sprachen'
and skillgroup.idskillgroup = 'SoftSkills'
但我无法让HQL为我生成这个...... “Sprachen”和“SoftSkills”是两个字符串来自String []我将该方法作为参数。该方法目前如下所示:
public List<Employee> findEmployeeWithTwoSkillGroups(final String[] skillGroups) {
return template.find("from Employee e join e.skillGroups as s where s in ?", Arrays.asList(skillGroups).toString
().substring(1, Arrays.asList(skillGroups).toString().length()-1));
}
我将数组“强制转换”到列表中,在其上执行toString()
(所以我得到“[Sprachen,SoftSkills]”)并切断第一个和最后一个字符(所以我得到“Sprachen,SoftSkills “)。
我想问题是HQL在('Sprachen,SoftSkills')中生成“[...]。idskillgroup”,就像它将两个字符串视为一个字符串...
而我无法让它像我想要的那样工作:/
有人可以帮助我并给我一个提示下一步尝试/做什么的提示吗? :-)
格尔茨 gilaras
Simmilar问题:
答案 0 :(得分:1)
正确的HQL语句应该或多或少地像这样:
from Employee e join e.skillGroups as s where s in ( 'Foo', 'Bar', 'Baz' )
所以你错过了每个单词的''。
编辑:现在我得到了你想要实现的目标: - )
我建议你这样做:
Query query = session.createQuery(
"from Employee e join e.skillGroups as s where s in (:skillGroups)");
// skillGroups must be a Collection type
query.setParameterList("skillGroups", skillGroups);
List list = query.list();
如果您需要将结果作为AND
数组所有元素的String[]
,则可以执行以下操作:
Query query = session.createQuery(
"from Employee e join e.skillGroups as s where s in (:skillGroups) group by e having count(s) = :skillGroupsLength");
query.setParameterList("skillGroups", skillGroups);
query.setParameter("skillGroupsLength", skillGroups.length);
List list = query.list();
答案 1 :(得分:0)
这是一个准备好的语句,而不是字符串连接。它处理('Sprachen,SoftSkills')就像一个字符串,因为它是一个字符串。您想在查询中插入一个集合。我不知道如何在hibernateTemplate中执行此操作,但hibernate本身支持添加集合。见the hibernate documentation。