我有一个 native Mysql
查询
select tl_id,c_name,m_name,u_first_name,t_name,
tl_logged_at,tl_minutes,tl_description
from users inner join clients on u_id=c_frn_owner_id
inner join matters on m_frn_client_id = c_id
inner join tasks on t_frn_matter_id = m_id
inner join task_logs on tl_frn_task_id = t_id
where c_id =2 and m_id=4 and t_id= 3 and u_id = 4
我可以为此编写JPQL
而不是 native 查询。但是如何使用JPA规范
获得相同的查询,因为where
条件中的列或字段c_id
,m_id
、t_id
和 u_id
是可选的。它们是提供给用户的过滤选项。
可以使用 if
条件生成 native 查询。但它们很容易出现拼写错误和 SQL 注入(inject)。
JPASpecification
的文档没有与连接多个表相关的信息。
Spring Official JPA Specification Doc
我对JPASpecification
非常陌生,因此任何指导都会很有值(value)。
最佳答案
JPA 2 introduces a criteria API that you can use to build queries programmatically.
如果您使用 JPA 2 并且想要创建动态查询,我认为您应该查看 Criteria API构建动态查询。或者你可以使用JpaSpecificationExecutor(我还没有尝试过。)检查this相关问题。
附加一些有用的链接:
关于mysql - 在 Spring Boot 中生成动态查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50116796/