Passing Arguments with DataReadQuery

27 Views Asked by At

We use EcipseLink with the basic JPA methods, but I'm trying to solve a problem regarding large resultsets so I'm looking into trying DataReadQuery and a streaming result set. That's my end goal but I can't get a basic query to run. Documentation seems pretty sparse online so I'm mostly winging it.

Session session = manager.unwrap(Session.class);
DataReadQuery query = new DataReadQuery("select first_name from user_table where user_id = ?");
List<String> arguments = new ArrayList<>();
arguments.add("Bob");
session.executeQuery(query, arguments);

This is a native query, not JPQL if that is important.

It throws The number of arguments provided to the query for execution does not match the number of arguments in the query definition. There's only 1 argument. It seems I'm clearly not passing in the value correctly.

1

There are 1 best solutions below

0
Chris On

Two potential problems with what you are doing.

  1. EclipseLink native queries used '#' to demarcate parameters in SQL strings. '?' is from JPA, and may work, but you should follow the docs when using the native Query objects.
  2. You've not indicated that the SQL string has parameters. You are required to call query.addArgument("ParameterName") to indicate the string has parameters and do so in the order you will be passing in values when executing the query.

See https://docs.oracle.com/cd/E15523_01/web.1111/b32441/qrybas.htm#CIHEBFID for examples