HHH-4567 - EntiytManager's QueryImpl mishandles ordinal position of HQL-style positional parameters

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17961 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-11-11 22:20:48 +00:00
parent 85b3ad297f
commit 49d529bad8
2 changed files with 38 additions and 4 deletions

View File

@ -75,7 +75,7 @@ public class QueryImpl<X> extends org.hibernate.ejb.AbstractQueryImpl<X> impleme
extractParameterInfo();
}
@SuppressWarnings({ "unchecked" })
@SuppressWarnings({ "unchecked", "RedundantCast" })
private void extractParameterInfo() {
if ( ! AbstractQueryImpl.class.isInstance( query ) ) {
throw new IllegalStateException( "Unknown query type for parameter extraction" );
@ -108,7 +108,7 @@ public class QueryImpl<X> extends org.hibernate.ejb.AbstractQueryImpl<X> impleme
final OrdinalParameterDescriptor descriptor =
queryImpl.getParameterMetadata().getOrdinalParameterDescriptor( i+1 );
ParameterImpl parameter = new ParameterImpl(
descriptor.getOrdinalPosition() + 1,
i + 1,
descriptor.getExpectedType() == null
? null
: descriptor.getExpectedType().getReturnedClass()
@ -204,7 +204,7 @@ public class QueryImpl<X> extends org.hibernate.ejb.AbstractQueryImpl<X> impleme
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "unchecked" })
@SuppressWarnings({ "unchecked", "RedundantCast" })
public List<X> getResultList() {
try {
return (List<X>) query.list();
@ -223,7 +223,7 @@ public class QueryImpl<X> extends org.hibernate.ejb.AbstractQueryImpl<X> impleme
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "unchecked" })
@SuppressWarnings({ "unchecked", "RedundantCast" })
public X getSingleResult() {
try {
boolean mucked = false;

View File

@ -212,6 +212,40 @@ public class QueryTest extends TestCase {
em.close();
}
public void testPositionalParameterForms() 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.getTransaction().commit();
em.getTransaction().begin();
// first using jpa-style positional parameter
Query query = em.createQuery( "select w from Wallet w where w.brand = ?1" );
query.setParameter( 1, "Lacoste" );
w = (Wallet) query.getSingleResult();
assertNotNull( w );
// next using jpa-style positional parameter, but as a name (which is how Hibernate core treats these
query = em.createQuery( "select w from Wallet w where w.brand = ?1" );
query.setParameter( "1", "Lacoste" );
w = (Wallet) query.getSingleResult();
assertNotNull( w );
// finally using hql-style positional parameter
query = em.createQuery( "select w from Wallet w where w.brand = ?" );
query.setParameter( 1, "Lacoste" );
w = (Wallet) query.getSingleResult();
assertNotNull( w );
em.remove( w );
em.getTransaction().commit();
em.close();
}
public void testNativeQuestionMarkParameter() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();