From 07c5967f460d77b5a8451996c4f8a76fe223f55f Mon Sep 17 00:00:00 2001 From: Emmanuel Bernard Date: Tue, 24 Nov 2009 09:39:08 +0000 Subject: [PATCH] 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 --- .../engine/query/ParameterMetadata.java | 4 ++- .../{ql => query}/ComponentJoinsTest.java | 2 +- .../ejb/test/{ => query}/QueryTest.java | 33 ++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) rename entitymanager/src/test/java/org/hibernate/ejb/test/{ql => query}/ComponentJoinsTest.java (98%) rename entitymanager/src/test/java/org/hibernate/ejb/test/{ => query}/QueryTest.java (93%) diff --git a/core/src/main/java/org/hibernate/engine/query/ParameterMetadata.java b/core/src/main/java/org/hibernate/engine/query/ParameterMetadata.java index 4d2cd9a2ad..81652165ea 100644 --- a/core/src/main/java/org/hibernate/engine/query/ParameterMetadata.java +++ b/core/src/main/java/org/hibernate/engine/query/ParameterMetadata.java @@ -76,7 +76,9 @@ public class ParameterMetadata implements Serializable { public OrdinalParameterDescriptor getOrdinalParameterDescriptor(int position) { 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]; } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ql/ComponentJoinsTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/query/ComponentJoinsTest.java similarity index 98% rename from entitymanager/src/test/java/org/hibernate/ejb/test/ql/ComponentJoinsTest.java rename to entitymanager/src/test/java/org/hibernate/ejb/test/query/ComponentJoinsTest.java index b61e1c77f6..87d33d721c 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/ql/ComponentJoinsTest.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/query/ComponentJoinsTest.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA\ */ -package org.hibernate.ejb.test.ql; +package org.hibernate.ejb.test.query; import javax.persistence.EntityManager; diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/QueryTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/query/QueryTest.java similarity index 93% rename from entitymanager/src/test/java/org/hibernate/ejb/test/QueryTest.java rename to entitymanager/src/test/java/org/hibernate/ejb/test/query/QueryTest.java index a221703fb0..599399861a 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/QueryTest.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/query/QueryTest.java @@ -1,5 +1,5 @@ //$Id$ -package org.hibernate.ejb.test; +package org.hibernate.ejb.test.query; import java.util.ArrayList; import java.util.Date; @@ -9,6 +9,10 @@ import javax.persistence.Query; import javax.persistence.TemporalType; 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(); } + 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 { EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin();