HQL无法查询JSON数据

问题描述 投票:0回答:1

这是NativeSql的工作很好:session.createSQLQuery(select json_length(fav_goods) from customer where id=1).uniqueResult()

但是如果按照以下步骤将其更改为HQL,则会引发错误session.createQuery(select json_length(favGoods) from CustomerEntity where id=1).uniqueResult()

错误

Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'json_length' {originalText=json_length}
    \-[EXPR_LIST] SqlNode: 'exprList'
       \-[DOT] DotNode: 'customeren0_.fav_goods' {propertyName=favGoods,dereferenceType=PRIMITIVE,getPropertyPath=favGoods,path={synthetic-alias}.favGoods,tableAlias=customeren0_,className=cn.phyer.bishe.entity.CustomerEntity,classAlias=null}
          +-[IDENT] IdentNode: '{synthetic-alias}' {originalText={synthetic-alias}}
          \-[IDENT] IdentNode: 'favGoods' {originalText=favGoods}
 [select json_length(favGoods) from cn.phyer.bishe.entity.CustomerEntity where id=?1]
    at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:220)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:155)
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:600)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:709)
    ... 39 more
Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'json_length' {originalText=json_length}
    \-[EXPR_LIST] SqlNode: 'exprList'
       \-[DOT] DotNode: 'customeren0_.fav_goods' {propertyName=favGoods,dereferenceType=PRIMITIVE,getPropertyPath=favGoods,path={synthetic-alias}.favGoods,tableAlias=customeren0_,className=cn.phyer.bishe.entity.CustomerEntity,classAlias=null}
          +-[IDENT] IdentNode: '{synthetic-alias}' {originalText={synthetic-alias}}
          \-[IDENT] IdentNode: 'favGoods' {originalText=favGoods}

在实体类CustomerEntity,字段fav_goods被命名为favGoods

mysql json hibernate hql
1个回答
0
投票

JPQL(或HQL)不支持JSON函数。

请在这里找到所有支持的功能:

https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#hql-functions

您必须坚持使用SQL。

© www.soinside.com 2019 - 2024. All rights reserved.