需要有关 Hibernate 和 JPQL 语法的帮助
我是框架和休眠的新手,我有一个无法解决的简单问题,
public static void listMembers(int page,int pageSize,String[] fields,String[] values) {
List<Member> members = Member.find(query,params).fetch();
render(members);
}
我不确定如何编写查询和参数
因为我希望每个字段都根据相应的值进行检查,
如下所示(这会生成错误):
public static void listMembers(int page,int pageSize,String[] fields,String[] values) {
String query = "" ;
String[] params = new String[values.length*2] ;
int i = 0 ;
for(i = 0 ; i < fields.length-1 ; i+=2 ) {
query += "?"+(i+1)+" like ?"+(i+2)+" AND" ;
params[i] = fields[i] ;
params[i+1] = "%"+values[i]+"%" ;
}
query += "?"+(i+1)+" like ?"+(i+2) ;
params[i] = fields[i] ;
params[i+1] = values[i] ;
List<Member> members = Member.find(query,params).fetch();
render(members);
}
编辑: 这样查询将是:
?1 like ?2 AND ?3 like ?4 AND .....
并且参数将是:
field1,%value1%,field2,%value2%,...
I am new to play framework and hibernate , I have a simple problem that I am unable to solve
public static void listMembers(int page,int pageSize,String[] fields,String[] values) {
List<Member> members = Member.find(query,params).fetch();
render(members);
}
I am unsure how to write the query and the params
as I want each field to be checked against corresponding value
something like this ( which generates errors) :
public static void listMembers(int page,int pageSize,String[] fields,String[] values) {
String query = "" ;
String[] params = new String[values.length*2] ;
int i = 0 ;
for(i = 0 ; i < fields.length-1 ; i+=2 ) {
query += "?"+(i+1)+" like ?"+(i+2)+" AND" ;
params[i] = fields[i] ;
params[i+1] = "%"+values[i]+"%" ;
}
query += "?"+(i+1)+" like ?"+(i+2) ;
params[i] = fields[i] ;
params[i+1] = values[i] ;
List<Member> members = Member.find(query,params).fetch();
render(members);
}
Edit:
Such that the query will be :
?1 like ?2 AND ?3 like ?4 AND .....
and params will be:
field1,%value1%,field2,%value2%,...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为最好使用 setParameter() 和 EntityManager:
I think it's better to use setParameter() and EntityManager: