openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r899780 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/
Date Fri, 15 Jan 2010 19:35:17 GMT
Author: ppoddar
Date: Fri Jan 15 19:35:16 2010
New Revision: 899780

URL: http://svn.apache.org/viewvc?rev=899780&view=rev
Log:
OPENJPA-1464: Reparameterize with enum-valued parameters.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Employee.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java?rev=899780&r1=899779&r2=899780&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
Fri Jan 15 19:35:16 2010
@@ -271,8 +271,16 @@
                 setCollectionValuedParameter(result, (Collection)val, indices, 
                     key);
             } else {
-                for (int j : indices)
+                for (int j : indices) {
+                    if (val instanceof Enum) {
+                        if (_template.get(j) instanceof Integer) {
+                            val = ((Enum)val).ordinal();
+                        } else {
+                            val = ((Enum)val).name();
+                        }
+                    } 
                     result.put(j, val);
+                }
             }
         }
         return result;

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Employee.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Employee.java?rev=899780&r1=899779&r2=899780&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Employee.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Employee.java
Fri Jan 15 19:35:16 2010
@@ -24,6 +24,8 @@
 @Table(name="EMPLOYEE_PQC")
 
 public class Employee {
+    public enum Category {PERMANENT, CONTRACTOR, TEMP};
+    
 	@Id
 	@GeneratedValue
 	private long id;
@@ -37,6 +39,11 @@
 	private Address address;
 	
 	private boolean isManager;
+	
+	private Category status;
+	
+	@Enumerated(EnumType.ORDINAL)
+	private Category hireStatus;
 
 	public boolean isManager() {
         return isManager;
@@ -73,5 +80,21 @@
 	public long getId() {
 		return id;
 	}
+	
+    public Category getHireStatus() {
+        return hireStatus;
+    }
+    
+    public Category setHireStatus(Category status) {
+        return hireStatus;
+    }
+    
+    public Category getCurrentStatus() {
+        return status;
+    }
+    
+    public Category getCurrentStatus(Category status) {
+        return status;
+    }
 
 }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java?rev=899780&r1=899779&r2=899780&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
Fri Jan 15 19:35:16 2010
@@ -28,6 +28,7 @@
 import javax.persistence.EntityManager;
 import javax.persistence.LockModeType;
 import javax.persistence.Query;
+import javax.persistence.TypedQuery;
 
 import junit.framework.TestCase;
 
@@ -44,6 +45,7 @@
 import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.apache.openjpa.persistence.OpenJPAQuery;
+import org.apache.openjpa.persistence.jdbc.sqlcache.Employee.Category;
 
 /**
  * Tests correctness and performance of queries with and without Prepared Query Cacheing.
@@ -760,6 +762,24 @@
         em.getTransaction().rollback();
     }
     
+    public void testEnumParameter() {
+        String jpql = "select e from Employee e where e.status=:current and e.hireStatus=:hire";
+        EntityManager em = emf.createEntityManager();
+        
+        TypedQuery<Employee> q1 = em.createQuery(jpql, Employee.class);
+        assertEquals(JPQLParser.LANG_JPQL, OpenJPAPersistence.cast(q1).getLanguage());
+        List<Employee> emps = q1.setParameter("current", Category.PERMANENT)
+                                .setParameter("hire", Category.CONTRACTOR).getResultList();
+        
+        // do the same thing again, this time query should be cached
+        em.getTransaction().begin();
+        TypedQuery<Employee> q2 = em.createQuery(jpql, Employee.class);
+        assertEquals(QueryLanguages.LANG_PREPARED_SQL, OpenJPAPersistence.cast(q2).getLanguage());
+        List<Employee> emps2 = q2.setParameter("current", Category.PERMANENT)
+                            .setParameter("hire", Category.CONTRACTOR).getResultList();
+        em.getTransaction().rollback();
+    }
+    
     
     PreparedQueryCache getPreparedQueryCache() {
         return emf.getConfiguration().getQuerySQLCacheInstance();



Mime
View raw message