Java hibernate 限制.in 和,如何使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2719642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:04:24 来源:igfitidea点击:
hibernate restrictions.in with and, how to use?
提问by cometta
I have table like below
我有如下表
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
how to query with Restriction.in by combining below AND into one IN statement?
如何通过将下面的 AND 组合成一个 IN 语句来使用 Restriction.in 进行查询?
IN[ (if(survey_no==1) && employee_no== 'test') ,
(if(survey_no==1) && employee_no== 'test2') ,
...
]
采纳答案by Daff
I think this is the criteria combination you want to use (btw. it is easier to help with the Hibernate entity bean definition instead of the table structure):
我认为这是您要使用的标准组合(顺便说一句。帮助 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();
回答by Vinit Jordan
List<EmployeeSurvey> employeeSurvey = getSession().createCriteria
(EmployeeSurvey.class).add(
Restrictions.and
(
Restrictions.eq("survey_no", 1),
Restrictions.in("employee_name", new String[] {"test", "test1"})
)).list();