My Oracle Support Banner

Setting Parameters Using CreateNativeQuery (Doc ID 389854.1)

Last updated on FEBRUARY 13, 2024

Applies to:

Oracle Containers for J2EE - Version 10.1.3.0.0 and later
Information in this document applies to any platform.

Goal

The @NamedQuery annotation defines EJB-QL queries for which the :param syntax is valid.
I wanted to use the @NamedNativeQuery (not @NamedQuery) annotation to define native SQL and not
use EJB-QL. Therefore, I tried the same syntax with @NamedNativeQuery, but it did not work.
the following code was used:

public List<Employee> findByName(String name) {
Query queryEmpByName = em.createNativeQuery("select empno, ename, sal,
hire_date from emp where upper(ename) like upper(?1)");
queryEmpByName.setParameter(1, name);
return queryEmpByName.getResultList();
}

and now a NullPointerException is generated.

How to use the param syntax with NamedNativeQuery?

Solution

To view full details, sign in with your My Oracle Support account.

Don't have a My Oracle Support account? Click to get started!


In this Document
Goal
Solution


My Oracle Support provides customers with access to over a million knowledge articles and a vibrant support community of peers and Oracle experts.