【问题标题】:hibernate restrictions.in with and, how to use?hibernate restricts.in with and,怎么用?
【发布时间】:2010-04-27 08:11:08
【问题描述】:

我有如下表

id, employee_no, survey_no, name
1    test          1         test_name
2    test2         1         test_name2
3    test3         1         test_name3
4    test4         2         test_name4

如何通过将下面的AND组合成一个IN语句来查询Restriction.in?

 IN[   (if(survey_no==1)  && employee_no== 'test')  ,  
       (if(survey_no==1)  && employee_no== 'test2') ,
        ...
   ]

【问题讨论】:

    标签: java sql hibernate


    【解决方案1】:

    我认为这是您要使用的标准组合(顺便说一句。帮助 Hibernate 实体 bean 定义而不是表结构更容易):

    String[] employeeNames = { "test", "test2" };
    List<Survey> surveys = getSession().createCriteria(Survey.class).add(
            Restrictions.and
            (
                Restrictions.eq("surveyNumber", 1),
                Restrictions.in("employeeName", employeeNames)
            )
        ).list();
    

    【讨论】:

    • 默认情况下,限制作为 AND 子句添加到条件中。在这种情况下,代码是正确的,但如果将这两个限制直接添加到条件而不是嵌套在 Restrictions.and 语句中,则会更简单。
    • 谢谢,很高兴知道。似乎我随后提出了一些比实际需要更复杂的查询。
    【解决方案2】:
    List<EmployeeSurvey> employeeSurvey = getSession().createCriteria
    (EmployeeSurvey.class).add(
        Restrictions.and
        (
            Restrictions.eq("survey_no", 1), 
            Restrictions.in("employee_name", new String[] {"test", "test1"})
        )).list();
    

    【讨论】:

    • 此答案因其长度和内容而被标记为低质量。您应该解释为什么这段代码可以解决所描述的问题。
    猜你喜欢
    • 2021-12-27
    • 2017-02-20
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2015-09-01
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多