它是hibernate bug吗?

时间:2013-08-12 13:19:30

标签: java hibernate

我写

    String sql = "select candidate_skill.candidate_id from candidate_skill " +
            "inner join skill on  skill.id = candidate_skill.skill_id  " +
            "where skill_id in (:skillIdList) group by candidate_skill.candidate_id " +
            "Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in (:skillIdList) )";
    sql = sql.replace(":skillIdList", generateSkillIdList(skills));
    Query query = session.createSQLQuery(sql);
    List<Candidate> candidates = query.list();

效果很好

第二种情况:

    String sql = "select candidate_skill.candidate_id from candidate_skill " +
            "inner join skill on  skill.id = candidate_skill.skill_id  " +
            "where skill_id in :skillIdList group by candidate_skill.candidate_id " +
            "Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in :skillIdList )";
    Query query = session.createSQLQuery(sql).setParameterList("skillIdList", skills);
    List<Candidate> candidates = query.list()

日志:

Hibernate: select candidate_skill.candidate_id from candidate_skill inner join skill on  skill.id = candidate_skill.skill_id  where skill_id in (?, ?) group by candidate_skill.candidate_id Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in ?, ? )

它不起作用

和第三:

String sql = "select candidate_skill.candidate_id from candidate_skill " +
                "inner join skill on  skill.id = candidate_skill.skill_id  " +
                "where skill_id in :skillIdList group by candidate_skill.candidate_id " +
                "Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in (:skillIdList) )";
        Query query = session.createSQLQuery(sql).setParameterList("skillIdList", skills);
        List<Candidate> candidates = query.list();

日志:

Hibernate: select candidate_skill.candidate_id from candidate_skill inner join skill on  skill.id = candidate_skill.skill_id  where skill_id in (?, ?) group by candidate_skill.candidate_id Having count(candidate_skill.candidate_id) = (select count(*) from skill where skill.id in (?, ?) )

效果很好

P.S。注意:skillIdList

周围的括号

如果我在查询中使用setParameterList(“argument”,value)和参数2次,那么第一次hibernate替换括号而在第二次 - 无

1 个答案:

答案 0 :(得分:2)

IN的语法需要括号。

至于为什么第三个例子正在起作用,两个猜测:

  • hibernate具有自动附加缺失括号的功能,但不知何故这不会扩展到子查询
  • sql server首先执行子选择,然后另一个查询对你传递的参数变得多余,因此不执行。