The following should work, but it is apparently broken:
String sql = "SELECT * FROM schema.Tabela WHERE integerColumn = ?" Query query = getEntityManager().createNativeQuery(sql, classe); Parameter p = new Parameter() { @Override public String getName() { return null; }
@Override public Integer getPosition() { return 1; }
@Override public Class getParameterType() { return Integer.TYPE; }
}; query.setParameter( p, null ); query.getResultList(); // also fails!!!
A workaround is to get the get underlying org.hibernate.Query and use its API:
String sql = "SELECT * FROM schema.Tabela WHERE integerColumn = ?" Query query = getEntityManager().createNativeQuery(sql, classe); q.unwrap( HibernateQuery.class ).getHibernateQuery() // gets org.hibernate.Query .setParameter( 0, // org.hibernate.Query parameters start at 0 null, // the null value StandardBasicTypes.INTEGER // the parameter type ); query.getResultList();
Note that the condition "integerColumn = ?" will always be false if null is bound (because a null value is considered "unknown"; an null (unknown) value will never equal another null (unknown) value. If the intention is for also check for a null value then the query should be:
"SELECT * FROM schema.Tabela WHERE integerColumn = ? OR integerColumn is null"
|