Postgres中的JSONB列的JPA查询

时间:2015-02-02 19:13:07

标签: java hibernate postgresql jpa

我正在尝试使用JPA为Postgres数据库中的JSONB列创建查询。该查询几乎与Postgres JSON Documentation

中的示例相似
-- Find documents in which the key "company" has value "Magnafone"
SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"company": "Magnafone"}';

其中jdoc是表api中的JSONB列。

到目前为止,执行此查询很容易,而不是问题。但是,如果我尝试参数化查询,我会陷入困境。

以下是我尝试过的一些问题:

1)这不起作用,因为?在'-signs:

之间无法识别
String name= "Magnafone";

JPA.em().createNativeQuery("SELECT jdoc FROM api
WHERE jdoc @> '{\"name\": ?}'", Record.class)
.setParameter(1, name)

错误:

    play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal p
arameters. Remember that ordinal parameters are 1-based! Position: 2]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10-2.2.3.jar:2.2.3]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10-2.2.3.jar:2.2.3]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:
2.2.3]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:
2.2.3]
        at scala.Option.map(Option.scala:145) [scala-library-2.10.3.jar:na]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3.applyOrElse(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:2.2.3]
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordin
al parameters are 1-based! Position: 2
        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:451) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at models.lol.player.IPlayer.getPlayerByName(IPlayer.java:22) ~[na:na]
        at controllers.LoadTeamController.loadTeamData(LoadTeamController.java:23) ~[na:na]
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:57) ~[na:na]
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:57) ~[na:na]
Caused by: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position:
 2
        at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:80) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Fina
l]
        at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:86) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Fi
nal]
        at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:450) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:422) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:445) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]</code>

2)我在另一个SO-Thread中找到了这个。虽然不起作用,因为||显示在查询中并抛出SQL异常。

JPA.em().createNativeQuery("SELECT jdoc FROM api 
WHERE jdoc @> '{\"name\": ' || :name ||'}'", Record.class)
.setParameter("name", name)

错误:

    play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal p
arameters. Remember that ordinal parameters are 1-based! Position: 2]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10-2.2.3.jar:2.2.3]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10-2.2.3.jar:2.2.3]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:
2.2.3]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:
2.2.3]
        at scala.Option.map(Option.scala:145) [scala-library-2.10.3.jar:na]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3.applyOrElse(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:2.2.3]
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordin
al parameters are 1-based! Position: 2
        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:451) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at models.lol.player.IPlayer.getPlayerByName(IPlayer.java:22) ~[na:na]
        at controllers.LoadTeamController.loadTeamData(LoadTeamController.java:23) ~[na:na]
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:57) ~[na:na]
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:57) ~[na:na]
Caused by: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position:
 2
        at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:80) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Fina
l]
        at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:86) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Fi
nal]
        at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:450) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:422) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:445) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]

我尝试过其他一些使Postgres或JPA以一种或另一种方式抛出的东西,并且在使用JPA的参数化时没有设法获得JSON参数。

3)

JPA.em().createNativeQuery("SELECT jdoc FROM api 
WHERE jdoc @> '{\"name\": ' || ? ||'}'", Record.class)
.setParameter(1", name)

错误:

    play.api.Application$$anon$1: Execution exception[[PersistenceException: org.hibernate.exception.DataException: FEHLER: ung³ltige Eingabesyntax f³r Typ json
  Detail: Die Eingabezeichenkette endete unerwartet.
  Position: 62
  Where: JSON-Daten, Zeile 1: {"name": ]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10-2.2.3.jar:2.2.3]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10-2.2.3.jar:2.2.3]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:
2.2.3]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:
2.2.3]
        at scala.Option.map(Option.scala:145) [scala-library-2.10.3.jar:na]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3.applyOrElse(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.3.jar:2.2.3]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.DataException: FEHLER: ung³ltige Eingabesyntax f³r Typ json
  Detail: Die Eingabezeichenkette endete unerwartet.
  Position: 62
  Where: JSON-Daten, Zeile 1: {"name":
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1377) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:266) ~[hibernate-entitymanager-4.1.8.Final.jar:4.1.8.Final]
        at models.lol.player.IPlayer.getPlayerByName(IPlayer.java:24) ~[na:na]
        at controllers.LoadTeamController.loadTeamData(LoadTeamController.java:23) ~[na:na]
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:57) ~[na:na]
Caused by: org.hibernate.exception.DataException: FEHLER: ung³ltige Eingabesyntax f³r Typ json
  Detail: Die Eingabezeichenkette endete unerwartet.
  Position: 62
  Where: JSON-Daten, Zeile 1: {"name":
        at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:134) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Final
]
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) ~[hibernate-core-4.1.8.Final.jar:4.1.8.
Final]
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Final]
        at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129) ~[hibernate-core-4.
1.8.Final.jar:4.1.8.Final]
        at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) ~[hibernate-core-4.1.8.Final.jar:4.1.8.Final]
Caused by: org.postgresql.util.PSQLException: FEHLER: ung³ltige Eingabesyntax f³r Typ json
  Detail: Die Eingabezeichenkette endete unerwartet.
  Position: 62
  Where: JSON-Daten, Zeile 1: {"name":
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157) ~[postgresql-9.2-1003-jdbc4.jar:na]
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886) ~[postgresql-9.2-1003-jdbc4.jar:na]
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) ~[postgresql-9.2-1003-jdbc4.jar:na]
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555) ~[postgresql-9.2-1003-jdbc4.jar:na]
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417) ~[postgresql-9.2-1003-jdbc4.jar:na]
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302) ~[postgresql-9.2-1003-jdbc4.jar:na]

仅供参考:这有效(不用担心,我没有使用它):

JPA.em().createNativeQuery("SELECT jdoc->'guid', jdoc->'name' FROM api 
WHERE jdoc @> '{\"company\": \""+company+"\"}'", Record.class)

另外:我使用的是createNativeQuery,因为JPA无法识别@> - 运算符。

感谢。

蒂莫

1 个答案:

答案 0 :(得分:0)

pozs将答案贴为评论:

  

尝试&#34; ... WHERE jdoc @&gt;演员(&#39; {\&#34;公司\&#34;:&#39; || to_json(演员(?作为文字))||&#39;}&#39;作为json)&# 34; (或jsonb在最后一次演员表中,如果那是合适的话)。