需要有关 Hibernate 和 JPQL 语法的帮助

发布于 2024-10-06 13:59:11 字数 1056 浏览 4 评论 0原文

我是框架和休眠的新手,我有一个无法解决的简单问题,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2024-10-13 13:59:12

我认为最好使用 setParameter() 和 EntityManager:

String jpql = "select m from Member m where ";
for (int i = 0; i < fields.length; i++) {
 jpql += (i > 0 ? " AND " : "") + fields[i] + " like '%?%' ";
}
Query q = entityManager.createQuery(jpql);
for (int i = 1; i <= values; i++) {
 q.setParameter(i, values[i]);
}
List<Members> members = q.getResultList();

I think it's better to use setParameter() and EntityManager:

String jpql = "select m from Member m where ";
for (int i = 0; i < fields.length; i++) {
 jpql += (i > 0 ? " AND " : "") + fields[i] + " like '%?%' ";
}
Query q = entityManager.createQuery(jpql);
for (int i = 1; i <= values; i++) {
 q.setParameter(i, values[i]);
}
List<Members> members = q.getResultList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文