[hibernate-commits] Hibernate SVN: r10382 - in trunk/HibernateExt/ejb/src: java/org/hibernate/ejb test/org/hibernate/ejb/test

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Aug 30 21:37:23 EDT 2006


Author: epbernard
Date: 2006-08-30 21:37:22 -0400 (Wed, 30 Aug 2006)
New Revision: 10382

Modified:
   trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java
   trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java
Log:
EJB-214 I look for positional versus plain ? parameters through a rudimentary algorithm. If people complain, I'll ahve to depreciate ?

Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java
===================================================================
--- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java	2006-08-31 01:18:15 UTC (rev 10381)
+++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java	2006-08-31 01:37:22 UTC (rev 10382)
@@ -7,17 +7,16 @@
 import java.util.List;
 import javax.persistence.FlushModeType;
 import javax.persistence.NoResultException;
+import javax.persistence.NonUniqueResultException;
 import javax.persistence.Query;
 import javax.persistence.TemporalType;
 import static javax.persistence.TemporalType.*;
 import javax.persistence.TransactionRequiredException;
-import javax.persistence.NonUniqueResultException;
 
 import org.hibernate.CacheMode;
 import org.hibernate.FlushMode;
 import org.hibernate.HibernateException;
 import org.hibernate.QueryParameterException;
-import org.hibernate.SQLQuery;
 import org.hibernate.TypeMismatchException;
 import org.hibernate.ejb.util.ConfigurationHelper;
 import org.hibernate.hql.QueryExecutionRequestException;
@@ -29,6 +28,7 @@
 public class QueryImpl implements Query, HibernateQuery {
 	private org.hibernate.Query query;
 	private HibernateEntityManagerImplementor em;
+	private Boolean isPositional = null;
 
 	public QueryImpl(org.hibernate.Query query, AbstractEntityManagerImpl em) {
 		this.query = query;
@@ -227,7 +227,7 @@
 
 	public Query setParameter(int position, Object value) {
 		try {
-			if ( isEJBQLQuery() ) {
+			if ( isPositionalParameter() ) {
 				this.setParameter( Integer.toString( position ), value );
 			}
 			else {
@@ -244,13 +244,30 @@
 		}
 	}
 
-	private boolean isEJBQLQuery() {
-		return ! ( query instanceof SQLQuery );
+	private boolean isPositionalParameter() {
+		if (isPositional == null) {
+			//compute it
+			String queryString = query.getQueryString();
+			int index = queryString.indexOf( '?' );
+			//there is a ? and the following char is a digit
+			if (index == -1) {
+				//no ?
+				isPositional = true;
+			}
+			else if ( index == queryString.length() - 1 ) {
+				// "... ?"
+				isPositional = false;
+			}
+			else {
+				isPositional = Character.isDigit( queryString.charAt( index + 1 ) );
+			}
+		}
+		return isPositional;
 	}
 
 	public Query setParameter(int position, Date value, TemporalType temporalType) {
 		try {
-			if ( isEJBQLQuery() ) {
+			if ( isPositionalParameter() ) {
 				String name = Integer.toString( position );
 				this.setParameter( name, value, temporalType );
 			}
@@ -278,7 +295,7 @@
 
 	public Query setParameter(int position, Calendar value, TemporalType temporalType) {
 		try {
-			if ( isEJBQLQuery() ) {
+			if ( isPositionalParameter() ) {
 				String name = Integer.toString( position );
 				this.setParameter( name, value, temporalType );
 			}

Modified: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java
===================================================================
--- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java	2006-08-31 01:18:15 UTC (rev 10381)
+++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java	2006-08-31 01:37:22 UTC (rev 10382)
@@ -2,8 +2,8 @@
 package org.hibernate.ejb.test;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
-import java.util.Date;
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
 import javax.persistence.TemporalType;
@@ -44,7 +44,7 @@
 		em.persist( item );
 		Query q = em.createQuery( "select count(i) from Item i where i.name like :itemName" );
 		q.setParameter( "itemName", "%" );
-		assertTrue( q.getSingleResult() instanceof Long);
+		assertTrue( q.getSingleResult() instanceof Long );
 		em.getTransaction().rollback();
 		em.close();
 	}
@@ -209,7 +209,7 @@
 		em.close();
 	}
 
-	public void testNativePositionalParameter() throws Exception {
+	public void testNativeQuestionMarkParameter() throws Exception {
 		EntityManager em = factory.createEntityManager();
 		em.getTransaction().begin();
 		Wallet w = new Wallet();
@@ -228,6 +228,34 @@
 		em.close();
 	}
 
+	public void testNativeQueryWithPositionalParameter() {
+
+		Item item = new Item( "Mouse", "Micro$oft mouse" );
+
+		EntityManager em = factory.createEntityManager();
+		em.getTransaction().begin();
+		em.persist( item );
+		assertTrue( em.contains( item ) );
+		em.getTransaction().commit();
+
+		em.getTransaction().begin();
+		Query query = em.createNativeQuery( "select * from Item where name = ?1", Item.class );
+		query.setParameter( 1, "Mouse" );
+		item = (Item) query.getSingleResult();
+		assertNotNull( item );
+		assertEquals( "Micro$oft mouse", item.getDescr() );
+		query = em.createNativeQuery( "select * from Item where name = ?", Item.class );
+		query.setParameter( 1, "Mouse" );
+		item = (Item) query.getSingleResult();
+		assertNotNull( item );
+		assertEquals( "Micro$oft mouse", item.getDescr() );
+		em.remove( item );
+		em.getTransaction().commit();
+
+		em.close();
+
+	}
+
 	public void testDistinct() throws Exception {
 		EntityManager em = factory.createEntityManager();
 		em.getTransaction().begin();
@@ -301,7 +329,11 @@
 		em.flush();
 		em.clear();
 
-		assertEquals( 1, em.createNativeQuery( "update Item i set i.descr = 'Logitech Mouse' where i.name = 'Mouse'").executeUpdate() );
+		assertEquals(
+				1, em.createNativeQuery(
+				"update Item i set i.descr = 'Logitech Mouse' where i.name = 'Mouse'"
+		).executeUpdate()
+		);
 		item = em.find( Item.class, item.getName() );
 		assertEquals( "Logitech Mouse", item.getDescr() );
 		em.remove( item );




More information about the hibernate-commits mailing list