简体   繁体   English

我有PIVOT时,使用JPA如何在本机查询中设置参数

[英]using JPA how to set parameter in native query when I have PIVOT

   q = createQuery( "  select * from ( ( SELECT * FROM ( SELECT dt.route_id , dt.amount , dc.card_type_id FROM DDRC_TRANS\n" +
                "dt , DDRC_CARD dc WHERE ( dt.card_id = dc.id AND dt.insert_time >= ('02-JAN-2000 04:00:00 AM')\n" + 
                "AND dt.insert_time <= ('02-FEB-2050 04:00:00 AM') AND dt.route_id IN ( '1', '3' ) ) ) PIVOT\n" + 
                "( SUM(amount ) FOR card_type_id IN ( 1,2,3 ) ) ) )"
                , clazz);

Ok Everything looks nice! 好的,一切看起来都不错! I can run this query and it returns the result too! 我可以运行此查询,它也返回结果!

But I have found one problem. 但是我发现了一个问题。 What about If I need to set parameter? 如果需要设置参数怎么办? for instance like this 例如这样

 q = createQuery( "  select * from ( ( SELECT * FROM ( SELECT dt.route_id , dt.amount , dc.card_type_id FROM DDRC_TRANS\n" +
                "dt , DDRC_CARD dc WHERE ( dt.card_id = dc.id AND dt.insert_time >= ('02-JAN-2000 04:00:00 AM')\n" + 
                "AND dt.insert_time <= ('02-FEB-2050 04:00:00 AM') AND dt.route_id IN ( '1', '3' ) ) ) PIVOT\n" + 
                "( SUM(amount ) FOR card_type_id IN ( :param ) ) ) )"
                , clazz);


         List<String> valueList = Arrays.asList("1,2,3");
         q.setParameter("param", valueList);

now here I have that type of error: 现在这里我有这种类型的错误:

java.sql.SQLException: ORA-56900: bind variable is not supported inside pivot|unpivot operation

why? 为什么? how can I fix this? 我怎样才能解决这个问题? Is this hibernate BUG? 这是休眠的BUG吗?

The problems looks like ojdbc does not allow to bind variables within PIVOT clause, because it is not supported . 问题似乎是ojdbc不允许在PIVOT子句中绑定变量,因为它不受支持

The first query you posted set the values explicit way. 您发布的第一个查询以显式方式设置值。 In the second, you are indirectly using preparedStatement through the JPA provider. 在第二个,你是间接使用preparedStatement时通过JPA提供商。 This is why you finally get a query statement that looks like follows which is not supported (note the parametrized values of IN clause) 这就是为什么您最终得到一个如下所示的查询语句的原因,该语句不受支持(请注意IN子句的参数化值)

select * .... PIVOT ( SUM(amount ) FOR card_type_id IN ( ? ) ) ) ) 

Although it won't work anyway, you must be aware that you are not setting a list of values . 尽管无论如何它都行不通,但您必须知道您没有设置值列表 In the query above you just set one parameter. 在上面的查询中,您只需设置一个参数。

For you native query (assuming that you are creating with EntityManager.createNativeQuery inside your createQuery method), 对于本机查询(假设您正在createQuery方法中使用EntityManager.createNativeQuery创建),

q = createQuery( "select * .... card_type_id IN ( :param ) ");
List<String> valueList = Arrays.asList("1,2,3");
q.setParameter("param", valueList);

what you are finally generating depends on how the JPA provider replace the :param by the valueList but it will be just one parameter replacement not a list. 最终生成的内容取决于JPA提供程序如何用valueList替换:param ,但这只是一个参数替换而不是列表。 For example, Hibernate will generate a preparedStatement like this 例如,Hibernate会生成一个preparedStatement时这样

select * .... card_type_id IN ( ? )

that finally takes next form after set set the valueList parameter, 设置了valueList参数后,最终采用下一种形式

select * .... card_type_id IN ( [1,2,3] )

What you really should achieve is a preparedStatement like this (note many parameters ? ) 您真正应该实现的是这样的prepareStatement(请注意许多参数?

select * .... card_type_id IN ( ?,?,? )

For do that the valueList should be as follows: 为此, valueList应该如下所示:

List<String> valueList = Arrays.asList("1","2","3");

Then the statement will take above prepared from and finally: 然后,该语句将采用上述内容,并最终完成:

select * .... card_type_id IN ( '1','2','3' )

You CANNOT pass a List in to JDBC as a parameter. 您不能将List作为参数传递给JDBC。 You have to pass in the individual values and have multiple "?" 您必须传递各个值并具有多个“?” in the statement. 在声明中。 Beyond that, you are down to the rules of the specific JDBC driver you are using and where it accepts parameters to be used. 除此之外,您还可以使用正在使用的特定JDBC驱动程序的规则,以及该规则接受要使用的参数的位置。 All JDBC drivers restrict where parameters can be applied in one way or another ... 所有JDBC驱动程序都限制可以以一种或另一种方式应用参数的位置...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM