Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用带字符串[]的HibernateTemplate.find(…)_Java_Sql_String_Hibernate_Hql - Fatal编程技术网

Java 使用带字符串[]的HibernateTemplate.find(…)

Java 使用带字符串[]的HibernateTemplate.find(…),java,sql,string,hibernate,hql,Java,Sql,String,Hibernate,Hql,我再次遇到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_

我再次遇到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”是字符串[]中的两个字符串,我将该方法作为参数提供。该方法当前如下所示:

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));
}
from Employee e join e.skillGroups as s where s in ( 'Foo', 'Bar', 'Baz' )
public List findEmployeewithtwo skillGroups(最终字符串[]skillGroups){
return template.find(“从员工e加入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”,就像它将两个字符串视为一个字符串一样…
我就是不能让它像我希望的那样工作:/

谁能帮帮我,给我一个下一步该做什么的提示吗?:-)

格里茨 吉拉拉斯

类似问题:


正确的HQL语句应该大致如下所示:

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));
}
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();
如果需要将结果作为
字符串[]
数组中所有元素的
,可以执行以下操作:

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();

这是一个准备好的语句,不是字符串连接。它像处理单个字符串一样处理('Sprachen,SoftSkills'),因为它是单个字符串。您希望在查询中插入集合。我不知道如何在hibernateTemplate中实现这一点,但hibernate本身支持添加集合。请参阅。

感谢您的编辑,我只是想知道如何在代码中换行:DHmmm,没错,但问题是如何让Hibernate生成这个,并在字符串中给出[];-)我没有一个单词,实际上我也不知道用户会搜索多少技能组(所以可能只有一个字符串,两个,三个,四个…),谢谢你的回答。这是我首先问的问题的正确答案,但我有点困惑,复制了错误的SQL。问题是发现所有员工都拥有所有技能组。。。所以必须有一个AND(IN给我一种OR)。。。你也能回答这个问题吗DYou应该认真研究HQL参考bro。都在那里。根据文档,对于和,您可以使用smth。像这样:
s=all elements(:skillGroups)
我通读了参考资料,没有找到任何解决我问题的方法。这就是我在这里问的原因;-)当我按照你说的方式做时,我得到
org.hibernate.hql.ast.QuerySyntaxException:expecting IDENT,found':'靠近第1行第110列[来自my.package.employeeweb.base.employee.employee.employee e将e.skillGroups作为s加入(所有元素(:skillGroups))][/code>同意,
所有元素都不起作用。然而,我在这个问题上花了一些时间,下面是我的解决方案:
来自员工e加入e.skillGroups as s,其中s在(:skillGroups)组中,e拥有count=:skillGroupsLength
。这意味着您需要用
字符串[]
数组的长度传递一个附加参数。这在我的盒子上很好用!;-)