Location parameter problem in Java – JPA Native Query

I'm trying to:

String sql = "SELECT email FROM users WHERE (type like 'B') AND (username like '?1')";
List results = em.createNativeQuery(sql).setParameter(1,username).getResultList();

But I get the illegalargumentexception, which tells me that the parameter is out of range What on earth did I do wrong?

Solution

There are no quotation marks around the parameter Try this:

String sql = "SELECT email FROM users WHERE (type like 'B') AND (username like ?1)";

You may also want to check carefully whether you really mean the type 'B', because it may not be in line with your idea

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>