【问题标题】:jpa custom query for multiple parametersjpa自定义查询多个参数
【发布时间】:2020-02-21 13:27:14
【问题描述】:

我需要创建一个 jpa 自定义查询,使用几个表上的连接来获取记录。

这是我想要实现的目标:

  1. 根据几个参数对数据进行排序(在运行时决定)
  2. 使用 where 子句过滤(在运行时决定)

例子:

@Query(value="Select a.name,b.city,c.reason from user a join address b on a.id=b.id join test c on a.id=c.id

where whereClause1 and whereClause2 //where子句可以是单个也可以是多个

order by orderByClause1 asc/desc //在运行时决定使用哪个参数进行排序"

List findData(String whereClause1,String whereClause2,String orderByClause1)

我无法为其创建通用查询。

我也可以接受任何其他方式来解决这个问题。 谢谢!!!

【问题讨论】:

标签: mysql hibernate spring-boot spring-data-jpa


【解决方案1】:

考虑到查询中的变量数量,您应该通过在运行时组合所有参数来生成查询并使用entityManager createQuery 方法执行查询,或者您可以使用hibernate criteria 创建查询

【讨论】:

    猜你喜欢
    • 2020-10-20
    • 1970-01-01
    • 2018-04-27
    • 2020-02-11
    • 2012-03-09
    • 1970-01-01
    • 2018-03-09
    • 2015-03-25
    • 1970-01-01
    相关资源
    最近更新 更多