Pages

Monday, June 6, 2011

JPA-style positional param was not an integral ordinal

My JPA named native query:

 @NamedNativeQuery(
   name = "getFailure",
   query = "SELECT * " +
   "FROM INTERFACE_FAILURES " +
   "WHERE server_id = ?1" +
   "AND interface_name = ?2" +
   "AND date_time = TO_DATE(?3, 'dd/MM/yyyy hh:mi:ss PM') ",
   resultClass = InterfaceFailure.class
 )

called as follows:

   Query query = entityManager.createNamedQuery("getFailure")
   .setParameter(1, serverId)
   .setParameter(2, interfaceId)
   .setParameter(3, dateTime);

   if(query.getResultList().size() > 0) {
     failure = (InterfaceFailure)query.getSingleResult();   
   }


results in the following:

15:54:33,438 ERROR [org.hibernate.impl.SessionFactoryImpl] Error in named query: getFailure: org.hibernate.QueryException: JPA-style positional param was not an integral ordinal
 at org.hibernate.engine.query.ParameterParser.parse(ParameterParser.java:111) [:3.6.0.Final]
 at org.hibernate.loader.custom.sql.SQLQueryParser.substituteParams(SQLQueryParser.java:290) [:3.6.0.Final]
...


The solution, which google did not help with at all, hence my post:

 @NamedNativeQuery(
   name = "getFailure",
   query = "SELECT * " +
   "FROM INTERFACE_FAILURES " +
   "WHERE server_id = (?1)" +
   "AND interface_name = (?2)" +
   "AND date_time = TO_DATE(?3, 'dd/MM/yyyy hh:mi:ss PM') ",
   resultClass = InterfaceFailure.class
 )


Add brackets around the integral positional parameters. 

8 comments:

Brad Cupit said...

As an alternative you could add white space before or after each string.

"WHERE server_id = ?1" +
"AND interface_name = ?2"

combines to become

"WHERE server_id = ?1AND interface_name = ?2"

(no space between ?1 and AND)

Unknown said...

Thanks for the solution that is not in Google

Bryan Neill said...

How Sway??

Unknown said...

Thanks, after googling, this was the solution.

JavaArch said...

This has saved my day. The no-space has caused errors and could not point it to anything. This post was really helpful.

Anonymous said...

thanks)

manjot singh said...

It is amazing and wonderful to visit your site. Thanks for sharing information; this is useful to student....

SASVBA provides the best R Programming Training In Delhiusing the latest development environment and framework in Delhi. We constantly update our program to reflect the latest industry trends. SASVBA is one of the top deep learning teaching institutes at NCR in Delhi that assists students in interviewing tech giants. We educate both college students and schoolchildren.

FOR MORE INFO:

Anonymous said...

smm panel
smm panel
İş İlanları Blog
instagram takipçi satın al
hirdavatciburada.com
Beyazesyateknikservisi.com.tr
Servis
tiktok jeton hilesi

Post a Comment

 
Powered by Blogger