Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 如何使用params在jpql中获取元组_Jpa_Jpql - Fatal编程技术网

Jpa 如何使用params在jpql中获取元组

Jpa 如何使用params在jpql中获取元组,jpa,jpql,Jpa,Jpql,现在我使用JPA访问数据库。我希望获得特定课程和特定课程中的注释,因此sql希望: select * from comment where (commentType, commentId) in (("course", "1"), ("lesson", 2)) @Query("select c from Comment c where (c.commentType, c.commentId) in :attaches") Page<Comment> findByAttachIn(@

现在我使用JPA访问数据库。我希望获得特定课程和特定课程中的注释,因此sql希望:

select * from comment where (commentType, commentId) in (("course", "1"), ("lesson", 2))
@Query("select c from Comment c where (c.commentType, c.commentId) in :attaches")
Page<Comment> findByAttachIn(@Param("attaches") List<String[]> attaches, Pageable pageable);
select * from comment where (commentType, commentId) in (?)
我使用annotation@Query如下:

select * from comment where (commentType, commentId) in (("course", "1"), ("lesson", 2))
@Query("select c from Comment c where (c.commentType, c.commentId) in :attaches")
Page<Comment> findByAttachIn(@Param("attaches") List<String[]> attaches, Pageable pageable);
select * from comment where (commentType, commentId) in (?)

JPA无法将数组从jql转换为sql。我该怎么办?

表达式中的
只允许单值路径表达式,因此您不能在JPQL查询中的
(commentType,commentId)中执行

表达式中的
只允许单值路径表达式,因此您不能执行
(commentType,commentId)在JPQL查询中。

JPQL BNF文档对于in部分非常清楚

in_expression ::= {state_field_path_expression | type_discriminator} [NOT] IN { ( in_item {, in_item}* ) | (subquery) | collection_valued_input_parameter } 
in_item ::= literal | single_valued_input_parameter
因此,可以得出结论,在JPQL中不能这样做。您可以在WHERE子句中手动执行此操作

... WHERE (commentType = :type1 AND commentId = :id1) OR 
          (commentType = :type2 AND commentId = :id2) OR 
          ...

长篇大论,但得到了答案,并符合JPQL BNF表示法。如何将标准JPQL转换为一些Spring语法留给熟悉非标准部分的人作为练习

JPQL BNF文档对于IN部分来说非常清楚

in_expression ::= {state_field_path_expression | type_discriminator} [NOT] IN { ( in_item {, in_item}* ) | (subquery) | collection_valued_input_parameter } 
in_item ::= literal | single_valued_input_parameter
因此,可以得出结论,在JPQL中不能这样做。您可以在WHERE子句中手动执行此操作

... WHERE (commentType = :type1 AND commentId = :id1) OR 
          (commentType = :type2 AND commentId = :id2) OR 
          ...


长篇大论,但得到了答案,并符合JPQL BNF表示法。如何将标准JPQL转换为一些Spring语法留给那些熟悉非标准部分的人作为练习

IIRC,元组两边需要有相同数量的“列”;左边有2个,但只有右边有。@Param(“attaches”)List attaches,每个字符串[]有2个元素。问题是JPA只将字符串[]视为一列。实际上是2。将其拆分为两个列表,每列一个?从注释中选择*,其中(commentType,commentId)在((“课程”,“1”),(“课程”,2))中我想得到这样的sql。因此,元组不能拆分。或者我可以通过其他sql?IIRC来实现,元组两边的“列”数必须相同;左边有2个,但只有右边有。@Param(“attaches”)List attaches,每个字符串[]有2个元素。问题是JPA只将字符串[]视为一列。实际上是2。将其拆分为两个列表,每列一个?从注释中选择*,其中(commentType,commentId)在((“课程”,“1”),(“课程”,2))中我想得到这样的sql。因此,元组不能拆分。或者我可以通过其他sql来实现它?那么,如果我想实现该功能,还有其他方法吗?既然您使用的是ID,为什么不直接使用
where-ID-in:idList
?从((“课程”,“1”),(“课程”,“2”)中的注释where(commentType,commentId)中选择*。commentType是条件之一。id不足…commentType是id为?的注释字段,是吗?选择by id只会产生一个特定的实体,因此在where子句中是否包含commentType并不重要。commentId不是注释的主键,id是。。。commentId是表course或lesson或其他内容中的外键。因此,如果我想实现此功能,还有其他方法吗?既然您使用的是ID,为什么不直接使用
where ID in:idList
?从((“课程”,“1”),(“课程”,“2”)中的注释where(commentType,commentId)中选择*呢。commentType是条件之一。id不足…commentType是id为?的注释字段,是吗?选择by id只会产生一个特定的实体,因此在where子句中是否包含commentType并不重要。commentId不是注释的主键,id是。。。commentId是table课程或课程或其他课程中的外键。我仍然不知道如何做。。。我不确定家里会有很多类型。而且。。。根本不支持代码。你的问题是如何在JPQL中实现这一点,这回答了这个问题(因为上面的语法是JPQL)。如何获得Spring数据来处理动态查询是一个单独的问题,我仍然不知道如何做。。。我不确定家里会有很多类型。而且。。。根本不支持代码。你的问题是如何在JPQL中实现这一点,这回答了这个问题(因为上面的语法是JPQL)。如何获得Spring数据来处理动态查询是一个单独的问题