gpt4 book ai didi

mysql - 如何使用EntityManager为MySql JSON_EXTRACT函数创建JPQL查询

转载 作者:行者123 更新时间:2023-11-30 21:36:53 24 4
gpt4 key购买 nike

我有 mysql 表,有一列使用 json 数据类型。我想以列作为条件执行查询使用JPQL,我可以使用EntityManager像下面这样编写。

它完全适用于以下(选择所有产品)

 Query query = em.createQuery("select o from Product o"); 

并且不适用于 json_extract 函数 + 实体管理器

 Query query = em.createQuery("select o from Product o where json_extract(o.des,'$.org') = 'ABC'");

这对我来说完全不起作用,并返回以下错误

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [select o from Product o where json_extract(o.des,'$.org') = 'ABC'].
[41, 85] The expression is not a valid conditional expression.

Caused by: org.eclipse.persistence.exceptions.JPQLException:
Exception Description: Syntax error parsing [select o from Product o where json_extract(o.des,'$.org') = 'ABC'].
[41, 85] The expression is not a valid conditional expression.

然后,我尝试了 CrudRepository 并且它有效,但我需要使用 EntityManager 而不是使用 CrudRepository

public interface ProductRepository extends CrudRepository<Product, String> {

@Query(value = "select o from Product o where json_extract(o.des,'$.org') = :org")
List<Product> findProdcutsByOrgJPQL(@Param("org") String org);
}

所以,我的JPQL没有问题。 EntityManager 出现问题。

如何使用 EntityManagerJSON_EXTRACT 函数创建 JPQL 查询

最佳答案

正如比利所说:

JPQL documentation has no such function with that name, so hardly surprising that an error occurs. If you want to ram in random SQL functions into JPQL then you use FUNCTION(...) as per all JPA docs tell you

我找到了解决方案:

Query query = em.createQuery("select o from Product o WHERE FUNCTION('JSON_EXTRACT', o.des, '$.org') = :org");

关于mysql - 如何使用EntityManager为MySql JSON_EXTRACT函数创建JPQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com