簡體   English   中英

HQL無法查詢JSON數據

[英]HQL cannot query JSON data

這是一個 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}

在實體 class CustomerEntity中,字段fav_goods被命名為favGoods

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

請在此處找到所有支持的功能:

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

您必須堅持使用 SQL。

我遇到了類似的問題,我不得不使用JSON_EXTRACT mysql function。

  1. 擴展MySQL5Dialect class 以在 ZF8CB4B31358FA17FA37CFAE6C2A0 中注冊 SQL Function。
public class CustomMySQLDialect extends MySQL5Dialect {
    public CustomMySQLDialect(){
        super();
        registerFunction(
                "JSON_EXTRACT",
                new StandardSQLFunction(
                        "JSON_EXTRACT",
                        StandardBasicTypes.STRING
                )
        );
    }
}
  1. 在 Hibernate cfg xml 中注冊自定義 MySQL 方言
<property name="hibernate.dialect">com.testsigma.specification.CustomMySQLDialect</property>
  1. 在 Hibernate 中使用CriteriaQuery (JPQL) 和 SQL Function。
Root<EntityType> subRoot = criteriaBuilder.from(Entity.class);
subQuery.select(builder.function("JSON_EXTRACT", String.class, subRoot.get("jsonData"), builder.literal("$.\"jsonPathField\"")));
query1.where(root.get("jsonKey").in(subQuery));

取自https://vladmihalcea.com/hibernate-sql-function-jpql-criteria-api-query/

從 Hibernate 5.2.3.Final 開始工作,Spring 數據 JPA 2.1.9.RELEASE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM