HHH-4604 IllegalArgumentException should be raised when an ordinal parameter is not present in the query

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18029 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-11-24 09:39:08 +00:00
parent 9d5f0d084a
commit 07c5967f46
3 changed files with 36 additions and 3 deletions

View File

@ -76,7 +76,9 @@ public class ParameterMetadata implements Serializable {
public OrdinalParameterDescriptor getOrdinalParameterDescriptor(int position) { public OrdinalParameterDescriptor getOrdinalParameterDescriptor(int position) {
if ( position < 1 || position > ordinalDescriptors.length ) { if ( position < 1 || position > ordinalDescriptors.length ) {
throw new IndexOutOfBoundsException( "Remember that ordinal parameters are 1-based!" ); String error = "Position beyond number of declared ordinal parameters. " +
"Remember that ordinal parameters are 1-based! Position: " + position;
throw new QueryParameterException( error );
} }
return ordinalDescriptors[position - 1]; return ordinalDescriptors[position - 1];
} }

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA\ * Boston, MA 02110-1301 USA\
*/ */
package org.hibernate.ejb.test.ql; package org.hibernate.ejb.test.query;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;

View File

@ -1,5 +1,5 @@
//$Id$ //$Id$
package org.hibernate.ejb.test; package org.hibernate.ejb.test.query;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -9,6 +9,10 @@ import javax.persistence.Query;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.ejb.test.TestCase;
import org.hibernate.ejb.test.Item;
import org.hibernate.ejb.test.Wallet;
import org.hibernate.ejb.test.Distributor;
/** /**
@ -246,6 +250,33 @@ public class QueryTest extends TestCase {
em.close(); em.close();
} }
public void testPositionalParameterWithUserError() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Wallet w = new Wallet();
w.setBrand( "Lacoste" );
w.setModel( "Minimic" );
w.setSerial( "0100202002" );
em.persist( w );
em.flush();
try {
Query query = em.createQuery( "select w from Wallet w where w.brand = ?1 and w.model = ?3" );
query.setParameter( 1, "Lacoste" );
query.setParameter( 2, "Expensive" );
query.getResultList();
fail("The query should fail due to a user error in parameters");
}
catch ( IllegalArgumentException e ) {
//success
}
finally {
em.getTransaction().rollback();
em.close();
}
}
public void testNativeQuestionMarkParameter() throws Exception { public void testNativeQuestionMarkParameter() throws Exception {
EntityManager em = getOrCreateEntityManager(); EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();