如何在postgres查询中转义:: JSONB?

时间:2017-09-05 05:56:58

标签: java json postgresql hibernate

我在执行查询时遇到异常

  

org.hibernate.QueryException:并未设置所有命名参数:
     [:jsonb]

这是我的疑问:

SELECT * FROM content c,resource r,organization_resource_relationship 
org,json_array_elements(c.attributes->'data') obj
WHERE c.resource_id=r.resource_id and r.account_id='633' and    
r.resource_id = org.resource_id and r.delete_status = false 
and ((obj->>'userId' in(select cast(resource_id as text) from resource 
 where resource_id=3181)  or  obj->>'signatureByCmisUsers'
 like '%3181%' or  obj->>'visibleToCmisUsers' like '%3181%' or  
 obj->>'editableByCmisUsers' like '%3181%' or  
 obj->>'obtainSignatureByCmisUsers' 
 like '%3181%' or  obj->>'documentVisibleToCmisUsers' like '%3181%' ) and 
  (obj->>'signatureStatus' like 'Pending'  or obj->>'signatureStatus'
  like 'PENDING'  or obj->>'signatureStatus' like 'SIGNED' or 
  obj->>'signatureStatus' like 'Signed') and   
 (obj->>'isDeleted'='false') 
   and ((obj->>'parentFolderId'='nil' or obj->>'parentFolderId' in 
     (select cast(resource_id as text) from
    resource re where re.delete_status=false))) and 
    ((obj->>'userType'='owner' or obj->>'userType'='receiver'))) 
    and ((((obj->>'visibleToRoles' like '%215%') and  
    ((obj->>'visibleToRoles') ::jsonb) ? 
    (select cast(resource_to_id as text) from resource_relationship where 
     resource_from_id=3181 and resource_to_id=215))))  
     and json_typeof(c.attributes->'data')='array' ORDER BY r.updated_on 
      DESC

如何解决问题?我需要使用::jsonb。但它引发了一个例外。

1 个答案:

答案 0 :(得分:0)

使用位置参数?1?2等。 Hibernate在遇到:时会感到困惑,并且没有"逃避"作为合法的Postgres铸造速记。

您也可以将其替换为CAST(obj->>'visibleToRoles' AS jsonb),但如果您更喜欢速记语法,则无法使用命名参数。

实际上,你为什么要施展它?为什么不使用->代替->>

很好的查询btw。 (那个讽刺,它看起来很糟糕,效率很低)。