From db2082dea48a90a5c6b3158bf459cb9844821a9d Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Tue, 30 Jun 2009 13:40:17 +0000 Subject: [PATCH] OPENJPA-1143: Added support for construct(), as(). Metamodel excludes static fields. tests edited to compile against EA3 JPA API. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@789723 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/criteria/CriteriaTest.java | 297 +++++++++--------- .../persistence/criteria/Magazine_.java | 1 - .../persistence/criteria/Publisher_.java | 1 - .../criteria/TestJoinCondition.java | 6 +- .../TestMetaModelTypesafeCriteria.java | 174 +++++----- .../persistence/criteria/TestSubqueries.java | 114 ++++--- .../criteria/TestTypeSafeCondExpression.java | 256 ++++++++------- .../criteria/TestTypesafeCriteria.java | 234 +++++++------- .../ImplicitFieldAccessMappedSuperclass_.java | 1 + .../persistence/meta/TestMetamodel.java | 31 +- .../openjpa/persistence/ResultItemImpl.java | 6 +- .../persistence/criteria/CriteriaBuilder.java | 153 ++++----- .../criteria/CriteriaExpressionBuilder.java | 59 ++-- .../criteria/CriteriaQueryImpl.java | 31 +- .../persistence/criteria/ExpressionImpl.java | 14 +- .../persistence/criteria/Expressions.java | 44 ++- .../persistence/criteria/FetchPathImpl.java | 3 +- .../openjpa/persistence/criteria/Joins.java | 9 +- .../criteria/NewInstanceSelection.java | 49 +++ .../persistence/criteria/ParameterImpl.java | 6 +- .../persistence/criteria/PathImpl.java | 76 +++-- .../persistence/criteria/PredicateImpl.java | 44 +-- .../persistence/criteria/SelectionImpl.java | 16 +- .../persistence/criteria/SubqueryImpl.java | 1 - .../persistence/meta/AbstractManagedType.java | 8 +- .../meta/AnnotationProcessor6.java | 8 +- .../meta/SourceAnnotationHandler.java | 5 +- 27 files changed, 826 insertions(+), 821 deletions(-) create mode 100644 openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/NewInstanceSelection.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/CriteriaTest.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/CriteriaTest.java index 395621c0e..49abc6191 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/CriteriaTest.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/CriteriaTest.java @@ -18,8 +18,11 @@ */ package org.apache.openjpa.persistence.criteria; +import java.io.PrintWriter; +import java.io.StringWriter; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -53,46 +56,48 @@ import org.apache.openjpa.persistence.test.AllowFailure; */ public abstract class CriteriaTest extends TestCase { protected static OpenJPAEntityManagerFactorySPI emf; + protected static SQLAuditor auditor; + QueryBuilder cb; EntityManager em; - protected List sql = new ArrayList(); + protected static Class[] CLASSES = { - Account.class, - Address.class, - A.class, - B.class, - CompUser.class, - Contact.class, - Contractor.class, - Course.class, - CreditCard.class, - Customer.class, - C.class, - Department.class, - DependentId.class, - Dependent.class, - D.class, - Employee.class, - Exempt.class, - FemaleUser.class, - FrequentFlierPlan.class, - Item.class, - LineItem.class, - Magazine.class, - MaleUser.class, - Manager.class, - Movie.class, - Order.class, - Person.class, - Phone.class, - Photo.class, - Product.class, - Publisher.class, - Semester.class, - Student.class, - TransactionHistory.class, - Transaction.class, - VideoStore.class}; + Account.class, + Address.class, + A.class, + B.class, + CompUser.class, + Contact.class, + Contractor.class, + Course.class, + CreditCard.class, + Customer.class, + C.class, + Department.class, + DependentId.class, + Dependent.class, + D.class, + Employee.class, + Exempt.class, + FemaleUser.class, + FrequentFlierPlan.class, + Item.class, + LineItem.class, + Magazine.class, + MaleUser.class, + Manager.class, + Movie.class, + Order.class, + Person.class, + Phone.class, + Photo.class, + Product.class, + Publisher.class, + Semester.class, + Student.class, + TransactionHistory.class, + Transaction.class, + VideoStore.class}; protected Class[] getDomainClasses() { return CLASSES; @@ -100,6 +105,7 @@ public abstract class CriteriaTest extends TestCase { public void setUp() { if (emf == null) { + auditor = new SQLAuditor(); createNamedEMF(getDomainClasses()); assertNotNull(emf); setDictionary(); @@ -107,6 +113,7 @@ public abstract class CriteriaTest extends TestCase { em = emf.createEntityManager(); cb = emf.getQueryBuilder(); } + /** * Create an entity manager factory for persistence unit pu. * Put {@link #CLEAR_TABLES} in @@ -120,13 +127,12 @@ public abstract class CriteriaTest extends TestCase { Map map = new HashMap(); map.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true," - + "SchemaAction='add,deleteTableContents')"); + + "SchemaAction='add')"); map.put("openjpa.jdbc.QuerySQLCache", "false"); map.put("openjpa.DynamicEnhancementAgent", "false"); map.put("openjpa.RuntimeUnenhancedClasses", "unsupported"); map.put("openjpa.Compatibility", "QuotedNumbersInQueries=true"); - map.put("openjpa.jdbc.JDBCListeners", - new JDBCListener[] { new Listener() }); + map.put("openjpa.jdbc.JDBCListeners", new JDBCListener[] { auditor }); StringBuffer buf = new StringBuffer(); for (Class c : types) { @@ -135,17 +141,11 @@ public abstract class CriteriaTest extends TestCase { buf.append(c.getName()); } - map.put("openjpa.MetaDataFactory", - "jpa(Types=" + buf.toString() + ")"); + map.put("openjpa.MetaDataFactory", "jpa(Types=" + buf.toString() + ")"); - Map config = new HashMap(System.getProperties()); + Map config = new HashMap(System.getProperties()); config.putAll(map); - emf = (OpenJPAEntityManagerFactorySPI) - Persistence.createEntityManagerFactory("test", config); - } - - public final void tearDown() { - // important: do nothing + emf = (OpenJPAEntityManagerFactorySPI) Persistence.createEntityManagerFactory("test", config); } void setDictionary() { @@ -156,57 +156,36 @@ public abstract class CriteriaTest extends TestCase { } /** - * Executes the given CriteriaQuery and JPQL string and compare their - * respective SQLs for equality. + * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs for equality. */ - void assertEquivalence(CriteriaQuery c, String jpql, String[] paramNames, - Object[] params) { + void assertEquivalence(CriteriaQuery c, String jpql) { + assertEquivalence(c, jpql, null); + } + + /** + * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs for equality. + * Sets the supplied parameters, if any. + */ + void assertEquivalence(CriteriaQuery c, String jpql, String[] paramNames, Object[] params) { Query cQ = em.createQuery(c); - for (int i = 0; i < paramNames.length; i++) { - cQ.setParameter(paramNames[i], params[i]); - } Query jQ = em.createQuery(jpql); - for (int i = 0; i < paramNames.length; i++) { - jQ.setParameter(paramNames[i], params[i]); - } - executeAndAssert(cQ, jQ); + setParameters(cQ, paramNames, params); + setParameters(jQ, paramNames, params); + + executeAndCompareSQL(jpql, cQ, jQ); } /** - * Executes the given CriteriaQuery and JPQL string and compare their - * respective SQLs for equality. + * Executes the given CriteriaQuery and JPQL string and compare their respective SQLs for equality. */ - void assertEquivalence(CriteriaQuery c, String jpql, Object[] params) { + void assertEquivalence(CriteriaQuery c, String jpql, Object[] params) { Query cQ = em.createQuery(c); - for (int i = 0; i < params.length; i++) { - cQ.setParameter(i + 1, params[i]); - } - Query jQ = em.createQuery(jpql); - for (int i = 0; i < params.length; i++) { - jQ.setParameter(i + 1, params[i]); - } + setParameters(cQ, params); + setParameters(jQ, params); - executeAndAssert(cQ, jQ); - } - - void assertEquivalence(CriteriaQuery c, String jpql) { - executeAndAssert(em.createQuery(c), em.createQuery(jpql)); - } - - void executeAndAssert(Query cQ, Query jQ) { - List[] sqls = new ArrayList[2]; - String jpql = OpenJPAPersistence.cast(jQ).getQueryString(); - if (!execute(jpql, cQ, jQ, sqls)) { - fail(sqlReport("Invalid SQL for Criteria",jpql, sqls[0], sqls[1])); - } - assertEquals(sqlReport("Unequal number of SQL ",jpql, sqls[0], sqls[1]) - , sqls[0].size(), sqls[1].size()); - for (int i = 0; i < sqls[0].size(); i++) - //sqlReport("Wrong SQL at " + i,jpql, sqls[0], sqls[1]), - assertEquals( - sqls[0].get(i), sqls[1].get(i)); + executeAndCompareSQL(jpql, cQ, jQ); } /** @@ -218,71 +197,82 @@ public abstract class CriteriaTest extends TestCase { * array. * @return true if both queries execute successfully. */ - boolean execute(String jpql, Query cQ, Query jQ, List[] sqls) { - sql.clear(); + void executeAndCompareSQL(String jpql, Query cQ, Query jQ) { + List jSQL = null; + List cSQL = null; try { - List jList = jQ.getResultList(); - } catch (PersistenceException e) { - e.printStackTrace(); - sqls[0] = new ArrayList(); - sqls[0].add(extractSQL(e)); - fail("Wrong SQL for JPQL :" + jpql + "\r\nSQL :" + sqls[0]); + jSQL = executeQueryAndCollectSQL(jQ); } catch (Exception e) { - e.printStackTrace(); - fail("Wrong JPQL :" + jpql); + StringWriter w = new StringWriter(); + e.printStackTrace(new PrintWriter(w)); + fail("JPQL " + jpql + " failed to execute\r\n" + w); } - sqls[0] = new ArrayList(sql); - - sql.clear(); try { - List cList = cQ.getResultList(); - } catch (PersistenceException e) { - e.printStackTrace(); - sqls[1] = new ArrayList(); - sqls[1].add(extractSQL(e)); - return false; + cSQL = executeQueryAndCollectSQL(cQ); + } catch (Exception e) { + StringWriter w = new StringWriter(); + e.printStackTrace(new PrintWriter(w)); + fail("CriteriaQuery corresponding to " + jpql + " failed to execute\r\n" + w); } - sqls[1] = new ArrayList(sql); - - return true; - } - - List executeJPQL(String jpql, Map params) { - sql.clear(); - Query q = em.createQuery(jpql); - if (params != null) { - for (Object key : params.keySet()) { - if (key instanceof String) - q.setParameter(key.toString(), params.get(key)); - else if (key instanceof String) - q.setParameter(key.toString(), params.get(key)); - else - throw new RuntimeException("Bad Parameter key " + key); + + printSQL("Target SQL for JPQL", jSQL); + printSQL("Target SQL for CriteriaQuery", cSQL); + if (jSQL.size() != cSQL.size()) { + printSQL("Target SQL for JPQL", jSQL); + printSQL("Target SQL for CriteriaQuery", cSQL); + assertEquals("No. of SQL generated for JPQL and CriteriaQuery for " + jpql + " is different", + jSQL.size(), cSQL.size()); + } + + for (int i = 0; i < jSQL.size(); i++) { + if (!jSQL.get(i).equals(cSQL.get(i))) { + printSQL("Target SQL for JPQL", jSQL); + printSQL("Target SQL for CriteriaQuery", cSQL); + assertEquals(i + "-th SQL for JPQL and CriteriaQuery for " + jpql + " is different", + jSQL.get(i), cSQL.get(i)); } } - - return sql; + } + + void printSQL(String header, List sqls) { + System.err.println(header); + for (int i = 0; sqls != null && i < sqls.size(); i++) { + System.err.println(i + ":" + sqls.get(i)); + } + } + + void setParameters(Query q, String[] paramNames, Object[] params) { + for (int i = 0; paramNames != null && i < paramNames.length; i++) + q.setParameter(paramNames[i], params[i]); + } + + void setParameters(Query q, Object[] params) { + for (int i = 0; params != null && i < params.length; i++) + q.setParameter(i+1, params[i]); + } + + /** + * Execute the given query and return the generated SQL. + * If the query execution fail because the generated SQL is ill-formed, then raised exception should + * carry the ill-formed SQL for diagnosis. + */ + List executeQueryAndCollectSQL(Query q) { + auditor.clear(); + try { + List result = q.getResultList(); + } catch (Exception e) { + throw new RuntimeException(extractSQL(e), e); + } + assertFalse(auditor.getSQLs().isEmpty()); + return auditor.getSQLs(); } - String extractSQL(PersistenceException e) { + String extractSQL(Exception e) { Throwable t = e.getCause(); if (t instanceof ReportingSQLException) return ((ReportingSQLException) t).getSQL(); - return null; - } - - private String sqlReport(String header, String jpql, List jSQLs, - List cSQLs) { - StringBuffer tmp = new StringBuffer(header).append("\r\n") - .append("JPQL:["+jpql+"]").append("\r\n"); - tmp.append(jSQLs.size() + " target SQL for JPQL").append("\r\n"); - for (String s : jSQLs) - tmp.append(s).append("\r\n"); - tmp.append(cSQLs.size() + " target SQL for Critera").append("\r\n"); - for (String s : cSQLs) - tmp.append(s).append("\r\n"); - return tmp.toString(); + return "Can not extract SQL from exception " + e; } @Override @@ -327,14 +317,23 @@ public abstract class CriteriaTest extends TestCase { } - public class Listener - extends AbstractJDBCListener { - - @Override - public void beforeExecuteStatement(JDBCEvent event) { - if (event.getSQL() != null && sql != null) { - sql.add(event.getSQL()); + public class SQLAuditor extends AbstractJDBCListener { + private List sqls = new ArrayList(); + + @Override + public void beforeExecuteStatement(JDBCEvent event) { + if (event.getSQL() != null && sqls != null) { + System.err.println("Adding " + event.getSQL()); + sqls.add(event.getSQL()); + } + } + + void clear() { + sqls.clear(); + } + + List getSQLs() { + return new ArrayList(sqls); } } } -} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/Magazine_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/Magazine_.java index 2554e1493..83b471d09 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/Magazine_.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/Magazine_.java @@ -14,5 +14,4 @@ public class Magazine_ { public static volatile SingularAttribute id; public static volatile SingularAttribute idPublisher; public static volatile SingularAttribute name; - public static volatile SingularAttribute serialVersionUID; } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/Publisher_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/Publisher_.java index c72857616..e6f901d4e 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/Publisher_.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/Publisher_.java @@ -13,5 +13,4 @@ public class Publisher_ { public static volatile SingularAttribute id; public static volatile SetAttribute magazineCollection; public static volatile SingularAttribute name; - public static volatile SingularAttribute serialVersionUID; } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJoinCondition.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJoinCondition.java index ae3384ce6..029e60ec8 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJoinCondition.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestJoinCondition.java @@ -37,21 +37,21 @@ public class TestJoinCondition extends CriteriaTest { CriteriaQuery cq = cb.createQuery(); Root c = cq.from(C.class); CollectionJoin d = c.join(C_.coll); - assertSame(Collection.class, d.getJavaType()); + assertSame(D.class, d.getJavaType()); } public void testSetJoinModel() { CriteriaQuery cq = cb.createQuery(); Root c = cq.from(C.class); SetJoin d = c.join(C_.set); - assertSame(Set.class, d.getJavaType()); + assertSame(D.class, d.getJavaType()); } public void testListJoinModel() { CriteriaQuery cq = cb.createQuery(); Root c = cq.from(C.class); ListJoin d = c.join(C_.list); - assertSame(List.class, d.getJavaType()); + assertSame(D.class, d.getJavaType()); } public void testInnerJoinSingleAttributeWithoutCondition() { diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestMetaModelTypesafeCriteria.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestMetaModelTypesafeCriteria.java index 228a30a37..2ba0ae173 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestMetaModelTypesafeCriteria.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestMetaModelTypesafeCriteria.java @@ -22,11 +22,14 @@ import java.math.BigDecimal; import java.util.List; import javax.persistence.Parameter; +import javax.persistence.Tuple; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Expression; import javax.persistence.criteria.Join; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.ListJoin; import javax.persistence.criteria.MapJoin; +import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.persistence.criteria.SetJoin; import javax.persistence.criteria.Subquery; @@ -139,7 +142,6 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { assertEquivalence(q, jpql); } - @AllowFailure(message="FetchJoin not implemented") public void testFetchJoins() { String jpql = "SELECT d FROM Department d LEFT JOIN FETCH d.employees " + "WHERE d.deptNo = 1"; @@ -158,30 +160,28 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { + "JOIN e.contactInfo.phones p " + "WHERE e.contactInfo.address.zipCode = '95054'"; - CriteriaQuery q = cb.createQuery(); - Root emp = q.from(Employee.class); - Join phone = emp.join( - employee_.getSingularAttribute("contactInfo", Contact.class)).join( - contact_.getList("phones", Phone.class)); - q.where(cb.equal(emp.get( - employee_.getSingularAttribute("contactInfo", Contact.class)).get( - contact_.getSingularAttribute("address", Address.class)).get( - address_.getSingularAttribute("zipCode", String.class)), "95054")); - q.select(phone.get(phone_.getSingularAttribute("vendor", String.class))); + + CriteriaQuery q = cb.createQuery(String.class); + Root e = q.from(Employee.class); + Join p = e.join(employee_.getSingularAttribute("contactInfo", Contact.class)) + .join(contact_.getList("phones", Phone.class)); + q.where(cb.equal(e.get(employee_.getSingularAttribute("contactInfo", Contact.class)) + .get(contact_.getSingularAttribute("address", Address.class)) + .get(address_.getSingularAttribute("zipCode", String.class)), + "95054")); + q.select(p.get(phone_.getSingularAttribute("vendor", String.class))); assertEquivalence(q, jpql); } public void testKeyPathNavigation() { - String jpql = "SELECT i.name, p FROM Item i JOIN i.photos p " - + "WHERE KEY(p) LIKE '%egret%'"; + String jpql = "SELECT i.name, p FROM Item i JOIN i.photos p WHERE KEY(p) LIKE '%egret%'"; - CriteriaQuery q = cb.createQuery(); - Root item = q.from(Item.class); - MapJoin photo = item.join( - item_.getMap("photos", String.class, Photo.class)); - q.multiselect(item.get(item_.getSingularAttribute("name", String.class)), photo) - .where(cb.like(photo.key(), "%egret%")); + CriteriaQuery q = cb.createTupleQuery(); + Root i = q.from(Item.class); + MapJoin p = i.join(item_.getMap("photos", String.class, Photo.class)); + q.multiselect(i.get(item_.getSingularAttribute("name", String.class)), p) + .where(cb.like(p.key(), "%egret%")); assertEquivalence(q, jpql); } @@ -191,23 +191,22 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { + "WHERE c.customer.accountNum = 321987 AND INDEX(t) BETWEEN 0 " + "AND 9"; - CriteriaQuery cq = cb.createQuery(); + CriteriaQuery cq = cb.createQuery(TransactionHistory.class); Root c = cq.from(CreditCard.class); - ListJoin t = c.join(creditCard_ - .getList("transactionHistory", TransactionHistory.class)); - cq.select(t).where( - cb.equal(c.get( - creditCard_.getSingularAttribute("customer", Customer.class)) - .get(customer_.getSingularAttribute("accountNum", Long.class)), - 321987), cb.between(t.index(), 0, 9)); + ListJoin t = c.join(creditCard_.getList("transactionHistory", + TransactionHistory.class)); + Predicate p1 = cb.equal( + c.get(creditCard_.getSingularAttribute("customer", Customer.class)) + .get(customer_.getSingularAttribute("accountNum", long.class)), 321987); + Predicate p2 = cb.between(t.index(), 0, 9); + cq.select(t).where(p1,p2); assertEquivalence(cq, jpql); } - @AllowFailure(message="as() not implemented") public void testIsEmptyExpressionOnJoin() { String jpql = "SELECT o FROM Order o WHERE o.lineItems IS EMPTY"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Order.class); Root o = q.from(Order.class); ListJoin lineItems = o.join(order_.getList("lineItems", LineItem.class)); @@ -221,19 +220,17 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { + "a.zipCode FROM Customer c JOIN c.orders o JOIN c.address a " + "WHERE a.state = 'CA' AND a.county = 'Santa Clara'"; - CriteriaQuery q = cb.createQuery(); - Root cust = q.from(Customer.class); - Join order = cust.join(customer_.getSet("orders", - Order.class)); - Join address = cust.join(customer_.getSingularAttribute( - "address", Address.class)); - q.where(cb.equal(address.get(address_.getSingularAttribute("state", - String.class)), "CA"), cb.equal(address.get(address_ - .getSingularAttribute("county", String.class)), "Santa Clara")); - q.multiselect(order.get(order_.getSingularAttribute("quantity", Integer.class)), - cb.prod(order.get(order_ - .getSingularAttribute("totalCost", Double.class)), 1.08), - address.get(address_.getSingularAttribute("zipCode", String.class))); + CriteriaQuery q = cb.createTupleQuery(); + Root c = q.from(Customer.class); + Join o = c.join(customer_.getSet("orders", Order.class)); + Join a = c.join(customer_.getSingularAttribute("address", Address.class)); + Expression taxedCost = cb.prod(o.get(order_.getSingularAttribute("totalCost", Double.class)), 1.08); + taxedCost.setAlias("taxedCost"); + q.where(cb.equal(a.get(address_.getSingularAttribute("state", String.class)), "CA"), + cb.equal(a.get(address_.getSingularAttribute("county", String.class)), "Santa Clara")); + q.multiselect(o.get(order_.getSingularAttribute("quantity", Integer.class)), + taxedCost, + a.get(address_.getSingularAttribute("zipCode", String.class))); assertEquivalence(q, jpql); } @@ -251,15 +248,12 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { String jpql = "SELECT w.name FROM Course c JOIN c.studentWaitList w " + "WHERE c.name = 'Calculus' AND INDEX(w) = 0"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(String.class); Root course = q.from(Course.class); - ListJoin w = course.join(course_.getList( - "studentWaitList", Student.class)); - q.where( - cb.equal( - course.get(course_.getSingularAttribute("name", String.class)), - "Calculus"), cb.equal(w.index(), 0)).select( - w.get(student_.getSingularAttribute("name", String.class))); + ListJoin w = course.join(course_.getList("studentWaitList", Student.class)); + q.where(cb.equal(course.get(course_.getSingularAttribute("name", String.class)), "Calculus"), + cb.equal(w.index(), 0)) + .select(w.get(student_.getSingularAttribute("name", String.class))); assertEquivalence(q, jpql); } @@ -274,10 +268,8 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { LineItem.class)); Join c = o.join(order_.getSingularAttribute("customer", Customer.class)); - q.where(cb - .equal(c.get(customer_.getSingularAttribute("lastName", String.class)), - "Smith"), cb.equal(c.get(customer_.getSingularAttribute( - "firstName", String.class)), "John")); + q.where(cb.equal(c.get(customer_.getSingularAttribute("lastName", String.class)), "Smith"), + cb.equal(c.get(customer_.getSingularAttribute("firstName", String.class)), "John")); q.select(cb.sum(i.get(lineItem_.getSingularAttribute("price", Double.class)))); assertEquivalence(q, jpql); @@ -312,18 +304,13 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { employee_.getSingularAttribute("department", Department.class)) .get(department_.getSingularAttribute("name", String.class)), "Engineering")); - q.multiselect(e.get(employee_.getSingularAttribute("name", String.class)), cb - .selectCase().when( - cb.equal(e.get(employee_.getSingularAttribute("rating", - Integer.class)), 1), - cb.prod(e.get(employee_.getSingularAttribute("salary", - Long.class)), 1.1)).when( - cb.equal(e.get(employee_.getSingularAttribute("rating", - Integer.class)), 2), - cb.prod(e.get(employee_.getSingularAttribute("salary", - Long.class)), 1.2)).otherwise( - cb.prod(e.get(employee_.getSingularAttribute("salary", - Long.class)), 1.01))); + q.multiselect(e.get(employee_.getSingularAttribute("name", String.class)), + cb.selectCase().when( + cb.equal(e.get(employee_.getSingularAttribute("rating", Integer.class)), 1), + cb.prod(e.get(employee_.getSingularAttribute("salary", Long.class)), 1.1)).when( + cb.equal(e.get(employee_.getSingularAttribute("rating", Integer.class)), 2), + cb.prod(e.get(employee_.getSingularAttribute("salary", Long.class)), 1.2)).otherwise( + cb.prod(e.get(employee_.getSingularAttribute("salary", Long.class)), 1.01))); assertEquivalence(q, jpql); } @@ -350,22 +337,21 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { assertEquivalence(q, jpql, new String[] { "stat" }, new Object[] { 1 }); } - @AllowFailure(message="Generates invalid SQL") public void testKeyExpressionInSelectList() { - String jpql = "SELECT v.location.street, KEY(i).title, VALUE(i) FROM " - + "VideoStore v JOIN v.videoInventory i " - + "WHERE v.location.zipCode = " + "'94301' AND VALUE(i) > 0"; + String jpql = "SELECT v.location.street, KEY(i).title, VALUE(i) " + + "FROM VideoStore v JOIN v.videoInventory i " + + "WHERE v.location.zipCode = '94301' AND VALUE(i) > 0"; + CriteriaQuery q = cb.createQuery(); Root v = q.from(VideoStore.class); - MapJoin inv = v.join(videoStore_.getMap( - "videoInventory", Movie.class, Integer.class)); - q.where(cb.equal(v.get( - videoStore_.getSingularAttribute("location", Address.class)).get( - address_.getSingularAttribute("zipCode", String.class)), "94301"), cb - .gt(inv.value(), 0)); + MapJoin i = v.join(videoStore_.getMap("videoInventory", Movie.class,Integer.class)); + q.where(cb.equal(v.get(videoStore_.getSingularAttribute("location", Address.class)) + .get(address_.getSingularAttribute("zipCode", String.class)), "94301"), + cb.gt(i.value(), 0)); q.multiselect(v.get(videoStore_.getSingularAttribute("location", Address.class)) - .get(address_.getSingularAttribute("street", String.class)), inv.key() - .get(movie_.getSingularAttribute("title", String.class)), inv.value()); + .get(address_.getSingularAttribute("street", String.class)), + i.key().get(movie_.getSingularAttribute("title", String.class)), + i.value()); assertEquivalence(q, jpql); } @@ -474,7 +460,6 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { assertEquivalence(q, jpql); } - @AllowFailure(message="Root of the subquery._delegate not set") public void testCorrelatedSubqueryWithAllClause() { String jpql = "SELECT o FROM Order o JOIN o.customer c " + "WHERE 10000 < ALL (SELECT a.balance FROM c.accounts a)"; @@ -557,30 +542,25 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest { } public void testOrderingWithNumericalExpressionInSelection() { - String jpql = "SELECT o.quantity, o.totalCost * 1.08 AS taxedCost, " - + "a.zipCode " + String jpql = "SELECT o.quantity, o.totalCost * 1.08 AS taxedCost, a.zipCode " + "FROM Customer c JOIN c.orders o JOIN c.address a " + "WHERE a.state = 'CA' AND a.county = 'Santa Clara' " + "ORDER BY o.quantity, taxedCost, a.zipCode"; CriteriaQuery q = cb.createQuery(); Root c = q.from(Customer.class); - Join o = c.join(customer_.getSet("orders", - Order.class)); - Join a = c.join(customer_.getSingularAttribute("address", - Address.class)); - q.where(cb.equal(a.get(address_.getSingularAttribute("state", String.class)), - "CA"), cb.equal(a.get(address_.getSingularAttribute("county", - String.class)), "Santa Clara")); - q.orderBy( - cb.asc(o.get(order_.getSingularAttribute("quantity", Integer.class))), - cb.asc(cb.prod(o.get(order_.getSingularAttribute("totalCost", - Double.class)), 1.08)), cb.asc(a.get(address_ - .getSingularAttribute("zipCode", String.class)))); - q.multiselect(o.get(order_.getSingularAttribute("quantity", Integer.class)), cb - .prod(o.get(order_.getSingularAttribute("totalCost", Double.class)), - 1.08), a.get(address_.getSingularAttribute("zipCode", - String.class))); + Join o = c.join(customer_.getSet("orders", Order.class)); + Join a = c.join(customer_.getSingularAttribute("address", Address.class)); + Expression taxedCost = cb.prod(o.get(order_.getSingularAttribute("totalCost", Double.class)), 1.08); + taxedCost.setAlias("taxedCost"); + q.where(cb.equal(a.get(address_.getSingularAttribute("state", String.class)), "CA"), + cb.equal(a.get(address_.getSingularAttribute("county", String.class)), "Santa Clara")); + q.orderBy(cb.asc(o.get(order_.getSingularAttribute("quantity", Integer.class))), + cb.asc(taxedCost), + cb.asc(a.get(address_.getSingularAttribute("zipCode", String.class)))); + q.multiselect(o.get(order_.getSingularAttribute("quantity", Integer.class)), + taxedCost, + a.get(address_.getSingularAttribute("zipCode", String.class))); assertEquivalence(q, jpql); } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestSubqueries.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestSubqueries.java index 1fccf3490..68b48ca64 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestSubqueries.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestSubqueries.java @@ -2,6 +2,7 @@ package org.apache.openjpa.persistence.criteria; import java.sql.Timestamp; +import javax.persistence.Tuple; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Expression; import javax.persistence.criteria.Join; @@ -20,7 +21,7 @@ public class TestSubqueries extends CriteriaTest { String query = "SELECT DISTINCT o.name FROM CompUser o WHERE EXISTS" + " (SELECT c FROM Address c WHERE c = o.address )"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(String.class); Root o = q.from(CompUser.class); Subquery
sq = q.subquery(Address.class); sq.correlate(o); @@ -28,7 +29,7 @@ public class TestSubqueries extends CriteriaTest { sq.select(c); sq.where(cb.equal(c, o.get(CompUser_.address))); q.where(cb.exists(sq)); -// q.select(o.get(CompUser_.name)).distinct(true); + q.select(o.get(CompUser_.name)).distinct(true); assertEquivalence(q, query); } @@ -38,7 +39,7 @@ public class TestSubqueries extends CriteriaTest { + " (SELECT s FROM CompUser s WHERE s.address.country = " + "o.address.country)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(String.class); Root o = q.from(CompUser.class); Subquery sq = q.subquery(CompUser.class); sq.correlate(o); @@ -47,7 +48,7 @@ public class TestSubqueries extends CriteriaTest { sq.where(cb.equal(s.get(CompUser_.address).get(Address_.country), o .get(CompUser_.address).get(Address_.country))); q.where(cb.exists(sq).negate()); -// q.select(o.get(CompUser_.name)).distinct(true); + q.select(o.get(CompUser_.name)).distinct(true); assertEquivalence(q, query); } @@ -58,9 +59,9 @@ public class TestSubqueries extends CriteriaTest { + " ANY (SELECT s.computerName " + " FROM CompUser s WHERE s.address.country IS NOT NULL)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(String.class); Root o = q.from(CompUser.class); -// q.select(o.get(CompUser_.name)); + q.select(o.get(CompUser_.name)); Subquery sq = q.subquery(String.class); Root s = sq.from(CompUser.class); sq.select(s.get(CompUser_.computerName)); @@ -77,9 +78,9 @@ public class TestSubqueries extends CriteriaTest { + " (select distinct o.id from LineItem i, Order o" + " where i.quantity > 10 and o.count > 1000 and i.id = o.id)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Integer.class); Root o1 = q.from(Order.class); -// q.select(o1.get(Order_.id)); + q.select(o1.get(Order_.id)); Subquery sq = q.subquery(Integer.class); Root i = sq.from(LineItem.class); @@ -98,9 +99,10 @@ public class TestSubqueries extends CriteriaTest { String query = "select o.id from Order o where o.customer.balanceOwed =" + " (select max(o2.customer.balanceOwed) from Order o2" + " where o.customer.id = o2.customer.id)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Integer.class); Root o2 = sq.from(Order.class); sq.where(cb.equal(o.get(Order_.customer).get(Customer_.id), o2.get( @@ -116,9 +118,10 @@ public class TestSubqueries extends CriteriaTest { String query = "select o from Order o where o.customer.balanceOwed =" + " (select max(o2.customer.balanceOwed) from Order o2" + " where o.customer.id = o2.customer.id)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Order.class); Root o = q.from(Order.class); -// q.select(o); + q.select(o); Subquery sq = q.subquery(Integer.class); Root o2 = sq.from(Order.class); sq.where(cb.equal(o.get(Order_.customer).get(Customer_.id), o2.get( @@ -133,9 +136,10 @@ public class TestSubqueries extends CriteriaTest { public void testSubquery04() { String query = "select o.id from Order o where o.quantity >" + " (select count(i) from o.lineItems i)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Long.class); Root osq = sq.correlate(o); Join i = osq.join(Order_.lineItems); @@ -146,9 +150,10 @@ public class TestSubqueries extends CriteriaTest { public void testSubquery05() { String query = "select o.id from Order o where o.quantity >" + " (select count(o.quantity) from Order o)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Long.class); Root o2 = sq.from(Order.class); q.where(cb.gt(o.get(Order_.quantity), sq.select(cb.count(o2 @@ -161,9 +166,10 @@ public class TestSubqueries extends CriteriaTest { public void testSubquery06() { String query = "select o.id from Order o where o.quantity >" + " (select count(o.id) from Order o)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Long.class); Root o2 = sq.from(Order.class); q.where(cb.gt(o.get(Order_.quantity), sq.select(cb.count(o2 @@ -176,9 +182,10 @@ public class TestSubqueries extends CriteriaTest { public void testSubquery07() { String query = "select o.id from Order o where o.quantity >" + " (select avg(o.quantity) from Order o)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Double.class); Root o2 = sq.from(Order.class); q.where(cb.gt(o.get(Order_.quantity), sq.select(cb.avg(o2 @@ -190,12 +197,12 @@ public class TestSubqueries extends CriteriaTest { @AllowFailure(message="JPQL generates invalid SQL") public void testSubquery08() { String query = "select c.name from Customer c " - + "where exists (select o from c.orders o where o.id = 1) " - + "or exists (select o from c.orders o where o.id = 2)"; + + "where exists(select o from c.orders o where o.id = 1) " + + "or exists(select o from c.orders o where o.id = 2)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(String.class); Root c = q.from(Customer.class); -// q.select(c.get(Customer_.name)); + q.select(c.get(Customer_.name)); Subquery sq1 = q.subquery(Order.class); Root c1 = sq1.correlate(c); SetJoin o1 = c1.join(Customer_.orders); @@ -217,9 +224,10 @@ public class TestSubqueries extends CriteriaTest { + "where o.quantity between " + "(select max(o.quantity) from Order o) and " + "(select avg(o.quantity) from Order o) "; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(String.class); Root c = q.from(Customer.class); -// q.select(c.get(Customer_.name)); + q.select(c.get(Customer_.name)); Subquery sq1 = q.subquery(Integer.class); Root o1 = sq1.from(Order.class); @@ -230,9 +238,7 @@ public class TestSubqueries extends CriteriaTest { sq2.select(cb.avg(o2.get(Order_.quantity))); SetJoin o = c.join(Customer_.orders); - // not sure how to do call between of integer(quantity) - // between integer (max quantity) and double (avg quantity) - // q.where(cb.between(o.get(Order_.quantity), sq1, sq2)); + q.where(cb.between(o.get(Order_.quantity), sq1, sq2.as(Integer.class))); assertEquivalence(q, query); } @@ -243,9 +249,10 @@ public class TestSubqueries extends CriteriaTest { + " (select sum(o2.quantity) from Customer c, " + "in(c.orders) o2) "; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Integer.class); Root c = sq.from(Customer.class); @@ -263,9 +270,10 @@ public class TestSubqueries extends CriteriaTest { + " (select avg(o2.quantity) from Customer c, in(c.orders) o2)" + " and (select min(o2.quantity) from Customer c, in(c.orders)" + " o2)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq1 = q.subquery(Double.class); Root c = sq1.from(Customer.class); @@ -277,21 +285,20 @@ public class TestSubqueries extends CriteriaTest { SetJoin o3 = c2.join(Customer_.orders); sq2.select(cb.min(o3.get(Order_.quantity))); - // do not know how to call between for double and integer - // q.where(cb.between(o2.get(Order_.quantity), sq1, sq2)); + q.where(cb.between(o2.get(Order_.quantity), sq1.as(Integer.class), sq2)); assertEquivalence(q, query); } public void testSubquery12() { - String query = - "select o.id from Customer c, in(c.orders)o " + String query = "select o.id from Customer c, in(c.orders)o " + "where o.quantity > (select sum(o2.quantity)" + " from c.orders o2)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root c = q.from(Customer.class); SetJoin o = c.join(Customer_.orders); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Integer.class); Root sqc = sq.correlate(c); @@ -307,10 +314,11 @@ public class TestSubqueries extends CriteriaTest { String query = "select o1.id, c.name from Order o1, Customer c" + " where o1.quantity = " + " any(select o2.quantity from in(c.orders) o2)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createTupleQuery(); Root o1 = q.from(Order.class); Join c = o1.join(Order_.customer); -// q.select(o1.get(Order_.id), c.get(Customer_.name)); + q.multiselect(o1.get(Order_.id), c.get(Customer_.name)); Subquery sq = q.subquery(Integer.class); Join sqc = sq.correlate(c); @@ -331,11 +339,11 @@ public class TestSubqueries extends CriteriaTest { + "(SELECT MAX(m3.id) FROM Magazine m3 " + "WHERE m3.idPublisher.id = p.id)) "; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root p = q.from(Publisher.class); Join m = p.join(Publisher_.magazineCollection, JoinType.LEFT); -// q.select(p, m); + q.multiselect(p, m); Subquery sq = q.subquery(Integer.class); Root m2 = sq.from(Magazine.class); @@ -376,9 +384,10 @@ public class TestSubqueries extends CriteriaTest { + " WHEN o2.quantity = 10 THEN false " + " ELSE false END from Order o2" + " where o.customer.id = o2.customer.id)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Boolean.class); Root o2 = sq.from(Order.class); @@ -402,9 +411,10 @@ public class TestSubqueries extends CriteriaTest { + " (select o.quantity*2 from LineItem i, Order o" + " where i.quantity > 10 and o.quantity > 1000 and i.id = " + "o.id)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o1 = q.from(Order.class); -// q.select(o1.get(Order_.id)); + q.select(o1.get(Order_.id)); Subquery sq = q.subquery(Integer.class); Root i = sq.from(LineItem.class); @@ -424,9 +434,10 @@ public class TestSubqueries extends CriteriaTest { String query = "select o.id from Order o where o.customer.name =" + " (select substring(o2.customer.name, 3) from Order o2" + " where o.customer.id = o2.customer.id)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.customer).get(Customer_.name)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(String.class); Root o2 = sq.from(Order.class); @@ -444,9 +455,10 @@ public class TestSubqueries extends CriteriaTest { public void testSubquery18() { String query = "select o.id from Order o where o.orderTs >" + " (select CURRENT_TIMESTAMP from o.lineItems i)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Integer.class); Root o = q.from(Order.class); -// q.select(o.get(Order_.id)); + q.select(o.get(Order_.id)); Subquery sq = q.subquery(Timestamp.class); Root o2 = sq.correlate(o); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java index 90db97109..b54352f0f 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypeSafeCondExpression.java @@ -37,11 +37,16 @@ package org.apache.openjpa.persistence.criteria; import javax.persistence.Parameter; +import javax.persistence.Tuple; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Expression; +import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.Root; +import javax.persistence.criteria.Selection; import javax.persistence.criteria.Subquery; +import org.apache.openjpa.persistence.inheritance.entity.ComputerUser; + /** * Tests type-strict version of Criteria API. The test scenarios are adapted * from TestEJBQLCondExpression in @@ -86,26 +91,24 @@ public class TestTypeSafeCondExpression extends CriteriaTest { } public void testInExpr() { - String jpql = "SELECT o.name FROM CompUser o " - + "WHERE o.age IN (29, 40, 10)"; + String jpql = "SELECT o.name FROM CompUser o WHERE o.age IN (29, 40, 10)"; - CriteriaQuery cq = cb.createQuery(); + CriteriaQuery cq = cb.createQuery(String.class); Root o = cq.from(CompUser.class); cq.where(cb.in(o.get(CompUser_.age)).value(29).value(40).value(10)); -// cq.select(o.get(CompUser_.name)); + cq.select(o.get(CompUser_.name)); assertEquivalence(cq, jpql); } public void testNotIn() { - String jpql = "SELECT o.name FROM CompUser o " - + "WHERE o.age NOT IN (29, 40, 10)"; + String jpql = "SELECT o.name FROM CompUser o WHERE o.age NOT IN (29, 40, 10)"; - CriteriaQuery cq = cb.createQuery(); + CriteriaQuery cq = cb.createQuery(String.class); Root o = cq.from(CompUser.class); cq.where(cb.in(o.get(CompUser_.age)).value(29).value(40).value(10) .negate()); -// cq.select(o.get(CompUser_.name)); + cq.select(o.get(CompUser_.name)); assertEquivalence(cq, jpql); } @@ -114,14 +117,14 @@ public class TestTypeSafeCondExpression extends CriteriaTest { String jpql = "SELECT o.computerName FROM CompUser o " + "WHERE o.name LIKE 'Sha%' AND o.computerName NOT IN ('PC')"; - CriteriaQuery cq = cb.createQuery(); + CriteriaQuery cq = cb.createQuery(String.class); Root o = cq.from(CompUser.class); cq.where(cb.and( cb.like(o.get(CompUser_.name),"Sha%"), cb.in(o.get(CompUser_.computerName)).value("PC").negate() )); -// cq.select(o.get(CompUser_.computerName)); + cq.select(o.get(CompUser_.computerName)); assertEquivalence(cq, jpql); } @@ -153,22 +156,20 @@ public class TestTypeSafeCondExpression extends CriteriaTest { } public void testLikeWithEscapeCharacter() { - String query = "SELECT o.name FROM CompUser o " - + "WHERE o.name LIKE :name ESCAPE '|'"; - CriteriaQuery cq = cb.createQuery(); - Root c = cq.from(CompUser.class); - Parameter param = cb.parameter(String.class, "name"); -// cq.where(cb.like(c.get(CompUser_.name), param, '|')); -// cq.select(c.get(CompUser_.name)); + String query = "SELECT o.name FROM CompUser o WHERE o.name LIKE :name ESCAPE '|'"; - assertEquivalence(cq, query, new String[]{"name"}, - new Object[] {"%|_%"}); + CriteriaQuery cq = cb.createQuery(String.class); + Root c = cq.from(CompUser.class); + ParameterExpression param = cb.parameter(String.class, "name"); + cq.where(cb.like(c.get(CompUser_.name), param, '|')); + cq.select(c.get(CompUser_.name)); + + assertEquivalence(cq, query, new String[]{"name"}, new Object[] {"%|_%"}); } public void testNullExpression() { - String query = - "SELECT o.name FROM CompUser o WHERE o.age IS NOT NULL AND " + - "o.computerName = 'PC' "; + String query = "SELECT o.name FROM CompUser o " + + "WHERE o.age IS NOT NULL AND o.computerName = 'PC'"; CriteriaQuery cq = cb.createQuery(String.class); Root c = cq.from(CompUser.class); @@ -195,8 +196,7 @@ public class TestTypeSafeCondExpression extends CriteriaTest { // do not support isEmpty for array fields public void testIsEmptyExprUsingCriteria() { - String query = - "SELECT o.name FROM CompUser o WHERE o.nicknames IS NOT EMPTY"; + String query = "SELECT o.name FROM CompUser o WHERE o.nicknames IS NOT EMPTY"; CriteriaQuery cq = cb.createQuery(String.class); Root o = cq.from(CompUser.class); @@ -208,17 +208,16 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testConstructorExprUsingCriteria() { - String query = - "SELECT NEW org.apache.openjpa.persistence.criteria.MaleUser(" + + String query = "SELECT NEW org.apache.openjpa.persistence.criteria.MaleUser(" + "c.name, c.computerName, c.address, c.age, c.userid)" + " FROM CompUser c WHERE c.name = 'Seetha'"; - CriteriaQuery cq = cb.createQuery(); + CriteriaQuery cq = cb.createQuery(MaleUser.class); Root c = cq.from(CompUser.class); cq.where(cb.equal(c.get(CompUser_.name), "Seetha")); -// cq.select(cb.select(MaleUser.class, c.get(CompUser_.name), -// c.get(CompUser_.computerName), c.get(CompUser_.address), -// c.get(CompUser_.age), c.get(CompUser_.userid))); + cq.select(cb.construct(MaleUser.class, c.get(CompUser_.name), + c.get(CompUser_.computerName), c.get(CompUser_.address), + c.get(CompUser_.age), c.get(CompUser_.userid))); assertEquivalence(cq, query); } @@ -389,11 +388,11 @@ public class TestTypeSafeCondExpression extends CriteriaTest { } public void testArithmFunc2() { - String query = - "select SQRT(e.age) From CompUser e WHERE e.name='Seetha'"; - CriteriaQuery q = cb.createQuery(); + String query = "select SQRT(e.age) From CompUser e WHERE e.name='Seetha'"; + + CriteriaQuery q = cb.createQuery(Double.class); Root e = q.from(CompUser.class); -// q.select(cb.sqrt(e.get(CompUser_.age))); + q.select(cb.sqrt(e.get(CompUser_.age))); q.where(cb.equal(e.get(CompUser_.name), "Seetha")); assertEquivalence(q, query); @@ -401,50 +400,46 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testArithmFunc3() { - String query = - "select MOD(e.age, 4) From CompUser e WHERE e.name='Seetha'"; - CriteriaQuery q = cb.createQuery(); + String query = "select MOD(e.age, 4) From CompUser e WHERE e.name='Seetha'"; + + CriteriaQuery q = cb.createQuery(Integer.class); Root e = q.from(CompUser.class); -// q.select(cb.mod(e.get(CompUser_.age), 4)); + q.select(cb.mod(e.get(CompUser_.age), 4)); q.where(cb.equal(e.get(CompUser_.name), "Seetha")); assertEquivalence(q, query); } public void testArithmFunc4() { - String query = "SELECT e.name FROM CompUser e WHERE " + - "SIZE(e.nicknames) = 6"; - CriteriaQuery q = cb.createQuery(); + String query = "SELECT e.name FROM CompUser e WHERE SIZE(e.nicknames) = 6"; + + CriteriaQuery q = cb.createQuery(String.class); Root e = q.from(CompUser.class); - //q.where(cb.equal(cb.size(e.get(CompUser_.nicknames)), 6)); -// q.select(e.get(CompUser_.name)); + q.where(cb.equal(cb.size(e.get(CompUser_.nicknames)), 6)); + q.select(e.get(CompUser_.name)); assertEquivalence(q, query); } public void testGroupByHavingClause() { - String query = - "SELECT c.name FROM CompUser c GROUP BY c.name HAVING c.name " + - "LIKE 'S%'"; + String query = "SELECT c.name FROM CompUser c GROUP BY c.name HAVING c.name LIKE 'S%'"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(String.class); Root e = q.from(CompUser.class); q.groupBy(e.get(CompUser_.name)); q.having(cb.like(e.get(CompUser_.name), "S%")); -// q.select(e.get(CompUser_.name)); + q.select(e.get(CompUser_.name)); assertEquivalence(q, query); } public void testOrderByClause() { - String query = - "SELECT c.name FROM CompUser c WHERE c.name LIKE 'S%' " + - "ORDER BY c.name"; + String query = "SELECT c.name FROM CompUser c WHERE c.name LIKE 'S%' ORDER BY c.name"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(String.class); Root e = q.from(CompUser.class); q.where(cb.like(e.get(CompUser_.name), "S%")); -// q.select(e.get(CompUser_.name)); + q.select(e.get(CompUser_.name)); q.orderBy(cb.asc(e.get(CompUser_.name))); assertEquivalence(q, query); @@ -454,9 +449,9 @@ public class TestTypeSafeCondExpression extends CriteriaTest { //To be Tested: AVG, COUNT, MAX, MIN, SUM String query = "SELECT AVG(e.age) FROM CompUser e"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Double.class); Root e = q.from(CompUser.class); -// q.select(cb.avg(e.get(CompUser_.age))); + q.select(cb.avg(e.get(CompUser_.age))); assertEquivalence(q, query); } @@ -464,9 +459,9 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testCOUNTAggregFunc() { String query = "SELECT COUNT(c.name) FROM CompUser c"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Long.class); Root e = q.from(CompUser.class); -// q.select(cb.count(e.get(CompUser_.name))); + q.select(cb.count(e.get(CompUser_.name))); assertEquivalence(q, query); } @@ -474,9 +469,9 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testMAXAggregFunc() { String query = "SELECT DISTINCT MAX(c.age) FROM CompUser c"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Integer.class); Root e = q.from(CompUser.class); -// q.select(cb.max(e.get(CompUser_.age))).distinct(true); + q.select(cb.max(e.get(CompUser_.age))).distinct(true); assertEquivalence(q, query); } @@ -484,9 +479,9 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testMINAggregFunc() { String query = "SELECT DISTINCT MIN(c.age) FROM CompUser c"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Integer.class); Root e = q.from(CompUser.class); -// q.select(cb.min(e.get(CompUser_.age))).distinct(true); + q.select(cb.min(e.get(CompUser_.age))).distinct(true); assertEquivalence(q, query); } @@ -494,20 +489,19 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testSUMAggregFunc() { String query = "SELECT SUM(c.age) FROM CompUser c"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Integer.class); Root e = q.from(CompUser.class); -// q.select(cb.sum(e.get(CompUser_.age))); + q.select(cb.sum(e.get(CompUser_.age))); assertEquivalence(q, query); } public void testTypeExpression1() { - String jpql = "SELECT e FROM CompUser e where TYPE(e) in (:a, :b) " + - "ORDER By e.name"; + String jpql = "SELECT e FROM CompUser e where TYPE(e) in (:a, :b) ORDER By e.name"; - CriteriaQuery cq = cb.createQuery(); + CriteriaQuery cq = cb.createQuery(CompUser.class); Root e = cq.from(CompUser.class); -// cq.select(e); + cq.select(e); Parameter param1 = cb.parameter(Class.class, "a"); Parameter param2 = cb.parameter(Class.class, "b"); cq.where(e.type().in(param1, param2)); @@ -520,27 +514,29 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testTypeExpression2() { String query = "SELECT TYPE(e) FROM CompUser e where TYPE(e) <> :t"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); Parameter param1 = cb.parameter(Class.class, "t"); -// q.select(e.type()); + Expression> etype = e.type(); + // how to specify the following + q.multiselect(e.type()); q.where(cb.equal(e.type(), param1).negate()); - assertEquivalence(q, query, new String[]{"t"}, - new Class[]{MaleUser.class}); + assertEquivalence(q, query, new String[]{"t"}, new Class[]{MaleUser.class}); } // Type literal // this Cartesian problem can not be rewritten to use JOIN public void testTypeExpression3() { - String query = "SELECT e, FemaleUser, a FROM Address a, FemaleUser e " - + " where e.address IS NOT NULL"; - CriteriaQuery q = cb.createQuery(); + String query = "SELECT e, FemaleUser, a FROM Address a, FemaleUser e where e.address IS NOT NULL"; + + CriteriaQuery q = cb.createTupleQuery(); Root
a = q.from(Address.class); - // Join e = a.join(Address_.user); - // q.select(cb.literal(FemaleUser.class), e.get(CompUser_.address)); - // q.where(cb.equal(e.type(), null).negate()); + Root e = q.from(FemaleUser.class); + q.multiselect(e, cb.literal(FemaleUser.class), e.get(CompUser_.address)); + q.where(e.get(FemaleUser_.address).isNotNull()); assertEquivalence(q, query); } @@ -548,9 +544,9 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testTypeExpression4() { String query = "SELECT e FROM CompUser e where TYPE(e) = MaleUser"; - CriteriaQuery cq = cb.createQuery(); + CriteriaQuery cq = cb.createQuery(CompUser.class); Root e = cq.from(CompUser.class); -// cq.select(e); + cq.select(e); cq.where(cb.equal(e.type(), cb.literal(MaleUser.class))); assertEquivalence(cq, query); @@ -581,9 +577,9 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testTypeExpression7() { String query = "SELECT TYPE(a.b) FROM A a"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root a = q.from(A.class); -// q.select(a.get(A_.b).type()); + q.multiselect(a.get(A_.b).type()); assertEquivalence(q, query); } @@ -591,9 +587,9 @@ public class TestTypeSafeCondExpression extends CriteriaTest { public void testTypeExpression8() { String query = "SELECT MaleUser FROM A a"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Class.class); Root a = q.from(A.class); -// q.select(cb.literal(MaleUser.class)); + q.multiselect(cb.literal(MaleUser.class)); assertEquivalence(q, query); } @@ -602,36 +598,34 @@ public class TestTypeSafeCondExpression extends CriteriaTest { String query = "SELECT " + " CASE TYPE(e) WHEN FemaleUser THEN 'Female' " + " ELSE 'Male' END FROM CompUser e"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(); Root e = q.from(CompUser.class); -// q.select(cb.selectCase(e.type()).when(FemaleUser.class, "Female") -// .otherwise("Male")); + q.select(cb.selectCase(e.type()) + .when(FemaleUser.class, "Female") + .otherwise("Male")); assertEquivalence(q, query); } public void testCoalesceExpressions() { - String query = "SELECT e.name, " - + "COALESCE (e.address.country, 'Unknown')" - + " FROM CompUser e ORDER BY e.name DESC"; + String query = "SELECT e.name, COALESCE (e.address.country, 'Unknown') FROM CompUser e ORDER BY e.name DESC"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); -// q.select(e.get(CompUser_.name), cb.coalesce().value( -// e.get(CompUser_.address).get(Address_.country)) -// .value("Unknown")); + q.multiselect(e.get(CompUser_.name), + cb.coalesce().value(e.get(CompUser_.address).get(Address_.country)).value("Unknown")); q.orderBy(cb.desc(e.get(CompUser_.name))); assertEquivalence(q, query); } public void testNullIfExpressions() { - String query = "SELECT e.name, NULLIF (e.address.country, 'USA')" - + " FROM CompUser e ORDER BY e.name DESC"; - CriteriaQuery q = cb.createQuery(); + String query = "SELECT e.name, NULLIF (e.address.country, 'USA') FROM CompUser e ORDER BY e.name DESC"; + + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); -// q.select(e.get(CompUser_.name), cb.nullif(e.get(CompUser_.address).get( -// Address_.country), "USA")); + q.multiselect(e.get(CompUser_.name), + cb.nullif(e.get(CompUser_.address).get(Address_.country), "USA")); q.orderBy(cb.desc(e.get(CompUser_.name))); assertEquivalence(q, query); @@ -642,14 +636,15 @@ public class TestTypeSafeCondExpression extends CriteriaTest { + "CASE e.address.country WHEN 'USA' THEN 'us' " + " ELSE 'non-us' END, e.address.country " + " FROM CompUser e"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); Expression cage = cb.sum(e.get(CompUser_.age), 1); Expression d2 = cb.selectCase( e.get(CompUser_.address).get(Address_.country)).when("USA", "us").otherwise("non-us"); -// q.select(e.get(CompUser_.name), cage, d2, e.get(CompUser_.address).get( -// Address_.country)); + q.multiselect(e.get(CompUser_.name), cage, d2, e.get(CompUser_.address).get( + Address_.country)); assertEquivalence(q, query); } @@ -660,15 +655,15 @@ public class TestTypeSafeCondExpression extends CriteriaTest { + " THEN 'United-States' " + " ELSE e.address.country END," + " e.address.country " + " FROM CompUser e"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); Expression cage = cb.sum(e.get(CompUser_.age), 1); Expression d2 = cb.selectCase( e.get(CompUser_.address).get(Address_.country)).when("USA", "United-States").otherwise( e.get(CompUser_.address).get(Address_.country)); -// q.select(e.get(CompUser_.name), cage, d2, e.get(CompUser_.address).get( -// Address_.country)); + q.multiselect(e.get(CompUser_.name), cage, d2, e.get(CompUser_.address).get( + Address_.country)); assertEquivalence(q, query); } @@ -679,11 +674,11 @@ public class TestTypeSafeCondExpression extends CriteriaTest { + " ELSE 'Male' END" + " FROM CompUser e WHERE e.name like 'S%' " + " ORDER BY e.name DESC"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); -// q.select(e.get(CompUser_.name), -// cb.selectCase(e.type()).when(FemaleUser.class, "Female") -// .otherwise("Male")); + q.multiselect(e.get(CompUser_.name), + cb.selectCase(e.type()).when(FemaleUser.class, "Female") + .otherwise("Male")); q.where(cb.like(e.get(CompUser_.name), "S%")); q.orderBy(cb.desc(e.get(CompUser_.name))); @@ -694,13 +689,13 @@ public class TestTypeSafeCondExpression extends CriteriaTest { String query = "SELECT e.name, CASE e.address.country WHEN 'USA'" + " THEN true ELSE false END," + " e.address.country FROM CompUser e"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); Expression b = cb.selectCase( e.get(CompUser_.address).get(Address_.country)).when("USA", true).otherwise(false); -// q.select(e.get(CompUser_.name), b, e.get(CompUser_.address).get( -// Address_.country)); + q.multiselect(e.get(CompUser_.name), b, e.get(CompUser_.address).get( + Address_.country)); assertEquivalence(q, query); } @@ -710,14 +705,14 @@ public class TestTypeSafeCondExpression extends CriteriaTest { + " CASE WHEN e.age > 30 THEN e.age - 1 " + " WHEN e.age < 15 THEN e.age + 1 ELSE e.age + 0 " + " END FROM CompUser e"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); Expression cage = cb.selectCase().when(cb.gt(e.get(CompUser_.age), 30), cb.diff(e.get(CompUser_.age), 1)).when( cb.lt(e.get(CompUser_.age), 15), cb.sum(e.get(CompUser_.age), 1)).otherwise( cb.sum(e.get(CompUser_.age), 0)); -// q.select(e.get(CompUser_.name), e.get(CompUser_.age), cage); + q.multiselect(e.get(CompUser_.name), e.get(CompUser_.age), cage); assertEquivalence(q, query); } @@ -729,7 +724,7 @@ public class TestTypeSafeCondExpression extends CriteriaTest { + " ELSE 'Non United-States' END," + " e.address.country " + " FROM CompUser e"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); Expression d2 = cb.selectCase() .when( @@ -738,8 +733,8 @@ public class TestTypeSafeCondExpression extends CriteriaTest { "USA"), "United-States").otherwise( "Non United-States"); Expression cage = cb.sum(e.get(CompUser_.age), 1); -// q.select(e.get(CompUser_.name), cage, d2, e.get(CompUser_.address).get( -// Address_.country)); + q.multiselect(e.get(CompUser_.name), cage, d2, e.get(CompUser_.address).get( + Address_.country)); assertEquivalence(q, query); } @@ -756,13 +751,13 @@ public class TestTypeSafeCondExpression extends CriteriaTest { + "org.apache.openjpa.persistence.criteria.CompUser$" + "CreditRating.EXCELLENT" + " END FROM CompUser e ORDER BY e.age"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); -// q.select(e.get(CompUser_.name), cb.selectCase().when( -// cb.equal(e.get(CompUser_.age), 11), CompUser.CreditRating.POOR) -// .when(cb.equal(e.get(CompUser_.age), 35), -// CompUser.CreditRating.GOOD).otherwise( -// CompUser.CreditRating.EXCELLENT)); + q.multiselect(e.get(CompUser_.name), cb.selectCase().when( + cb.equal(e.get(CompUser_.age), 11), CompUser.CreditRating.POOR) + .when(cb.equal(e.get(CompUser_.age), 35), + CompUser.CreditRating.GOOD).otherwise( + CompUser.CreditRating.EXCELLENT)); q.orderBy(cb.asc(e.get(CompUser_.age))); @@ -787,21 +782,20 @@ public class TestTypeSafeCondExpression extends CriteriaTest { + "CreditRating.EXCELLENT" + " END from CompUser e1" + " where e.userid = e1.userid) ORDER BY e.age"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(CompUser.class); -// q.select(e.get(CompUser_.name), e.get(CompUser_.creditRating)); + q.multiselect(e.get(CompUser_.name), e.get(CompUser_.creditRating)); q.orderBy(cb.asc(e.get(CompUser_.age))); - Subquery sq = q.subquery(Integer.class); + Subquery sq = q.subquery(Object.class); Root e1 = sq.from(CompUser.class); sq.where(cb.equal(e.get(CompUser_.userid), e1.get(CompUser_.userid))); q.where(cb.equal(e.get(CompUser_.creditRating), - // sq.select( - cb.selectCase().when(cb.equal(e1.get(CompUser_.age), 11), - CompUser.CreditRating.POOR).when( - cb.equal(e1.get(CompUser_.age), 35), - CompUser.CreditRating.GOOD).otherwise( - CompUser.CreditRating.EXCELLENT))); + sq.select( + cb.selectCase() + .when(cb.equal(e1.get(CompUser_.age), 11), CompUser.CreditRating.POOR) + .when(cb.equal(e1.get(CompUser_.age), 35), CompUser.CreditRating.GOOD) + .otherwise(CompUser.CreditRating.EXCELLENT)))); q.orderBy(cb.asc(e.get(CompUser_.age))); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java index 0bd443e09..001f59116 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java @@ -25,14 +25,17 @@ import java.util.List; import javax.persistence.Parameter; import javax.persistence.Tuple; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Expression; import javax.persistence.criteria.Join; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.ListJoin; import javax.persistence.criteria.MapJoin; +import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.Root; import javax.persistence.criteria.SetJoin; import javax.persistence.criteria.Subquery; import javax.persistence.criteria.QueryBuilder.Case; +import javax.persistence.criteria.QueryBuilder.SimpleCase; import org.apache.openjpa.persistence.test.AllowFailure; @@ -107,28 +110,29 @@ public class TestTypesafeCriteria extends CriteriaTest { public void testProjection() { String jpql = "select a.balance,a.loan from Account a"; - CriteriaQuery c = cb.createQuery(); + CriteriaQuery c = cb.createTupleQuery(); Root account = c.from(Account.class); -// c.select(account.get(Account_.balance), account.get(Account_.loan)); + c.multiselect(account.get(Account_.balance), account.get(Account_.loan)); assertEquivalence(c, jpql); } public void testAbsExpression() { - CriteriaQuery c = cb.createQuery(); + String jpql = "select a from Account a where abs(a.balance)=100"; + + CriteriaQuery c = cb.createQuery(Account.class); Root account = c.from(Account.class); - String jpql = "select a from Account a where abs(a.balance)=100"; -// c.select(account).where( -// cb.equal(cb.abs(account.get(Account_.balance)), 100)); + c.select(account).where(cb.equal(cb.abs(account.get(Account_.balance)), 100)); assertEquivalence(c, jpql); } public void testAvgExpression() { - CriteriaQuery c = cb.createQuery(); + String jpql = "select avg(a.balance) from Account a"; + + CriteriaQuery c = cb.createQuery(Double.class); Root account = c.from(Account.class); - String jpql = "select avg(a.balance) from Account a"; -// c.select(cb.avg(account.get(Account_.balance))); + c.select(cb.avg(account.get(Account_.balance))); assertEquivalence(c, jpql); } @@ -330,9 +334,9 @@ public class TestTypesafeCriteria extends CriteriaTest { public void testTypeExpression() { String jpql = "SELECT TYPE(e) FROM Employee e WHERE TYPE(e) <> Exempt"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(); Root emp = q.from(Employee.class); -// q.select(emp.type()).where(cb.notEqual(emp.type(), Exempt.class)); + q.multiselect(emp.type()).where(cb.notEqual(emp.type(), Exempt.class)); assertEquivalence(q, jpql); } @@ -372,30 +376,28 @@ public class TestTypesafeCriteria extends CriteriaTest { String jpql = "SELECT SIZE(d.employees) FROM Department d " + "WHERE d.name = 'Sales'"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Integer.class); Root d = q.from(Department.class); q.where(cb.equal(d.get(Department_.name), "Sales")); -// q.select(cb.size(d.get(Department_.employees))); + q.select(cb.size(d.get(Department_.employees))); assertEquivalence(q, jpql); } public void testCaseExpression() { - String jpql = - "SELECT e.name, CASE WHEN e.rating = 1 THEN e.salary * 1.1 " - + "WHEN e.rating = 2 THEN e.salary * 1.2 ELSE e.salary * " - + "1.01 END " + String jpql = "SELECT e.name, CASE " + + "WHEN e.rating = 1 THEN e.salary * 1.1 " + + "WHEN e.rating = 2 THEN e.salary * 1.2 " + + "ELSE e.salary * 1.01 END " + "FROM Employee e WHERE e.department.name = 'Engineering'"; CriteriaQuery q = cb.createQuery(); Root e = q.from(Employee.class); - q.where(cb.equal(e.get(Employee_.department).get(Department_.name), - "Engineering")); -// q.select(e.get(Employee_.name), cb.multiselect(Case().when( -// cb.equal(e.get(Employee_.rating), 1), -// cb.prod(e.get(Employee_.salary), 1.1)).when( -// cb.equal(e.get(Employee_.rating), 2), -// cb.prod(e.get(Employee_.salary), 1.2)).otherwise( -// cb.prod(e.get(Employee_.salary), 1.01)))); + q.where(cb.equal(e.get(Employee_.department).get(Department_.name), "Engineering")); + q.multiselect(e.get(Employee_.name), + cb.selectCase() + .when(cb.equal(e.get(Employee_.rating), 1), cb.prod(e.get(Employee_.salary), 1.1)) + .when(cb.equal(e.get(Employee_.rating), 2), cb.prod(e.get(Employee_.salary), 1.2)) + .otherwise(cb.prod(e.get(Employee_.salary), 1.01))); assertEquivalence(q, jpql); } @@ -440,14 +442,12 @@ public class TestTypesafeCriteria extends CriteriaTest { + "FROM Employee e WHERE e.department.name = 'Engineering'"; CriteriaQuery q = cb.createQuery(); Root e = q.from(Employee.class); - q.where(cb.equal(e.get(Employee_.department).get(Department_.name), - "Engineering")); -// q.select(e.get(Employee_.name), cb.multiselect(Case().when( -// cb.equal(e.get(Employee_.rating), 1), -// cb.prod(e.get(Employee_.salary), 1.1)).when( -// cb.equal(e.get(Employee_.rating), 2), -// cb.prod(e.get(Employee_.salary), 1.2)).otherwise( -// cb.prod(e.get(Employee_.salary), 1.01))); + q.where(cb.equal(e.get(Employee_.department).get(Department_.name), "Engineering")); + q.multiselect(e.get(Employee_.name), + cb.selectCase() + .when(cb.equal(e.get(Employee_.rating), 1), cb.prod(e.get(Employee_.salary), 1.1)) + .when(cb.equal(e.get(Employee_.rating), 2), cb.prod(e.get(Employee_.salary), 1.2)) + .otherwise(cb.prod(e.get(Employee_.salary), 1.01))); assertEquivalence(q, jpql); } @@ -457,14 +457,16 @@ public class TestTypesafeCriteria extends CriteriaTest { + "WHEN 1 THEN e.salary * 1.1 " + "WHEN 2 THEN e.salary * 1.2 ELSE e.salary * 1.01 END " + "FROM Employee e WHERE e.department.name = 'Engineering'"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(Employee.class); - q.where(cb.equal(e.get(Employee_.department).get(Department_.name), - "Engineering")); -// q.select(e.get(Employee_.name), cb.multiselect(Case(e.get(Employee_.rating)) -// .when(1, cb.prod(e.get(Employee_.salary), 1.1)).when(2, -// cb.prod(e.get(Employee_.salary), 1.2)).otherwise( -// cb.prod(e.get(Employee_.salary), 1.01))); + q.where(cb.equal(e.get(Employee_.department).get(Department_.name), "Engineering")); + Expression salary = e.get(Employee_.salary); + Expression rating = e.get(Employee_.rating); + q.multiselect(e.get(Employee_.name), + cb.selectCase(rating). + when(1, cb.prod(salary, 1.1)) + .when(2, cb.prod(salary, 1.2)) + .otherwise(cb.prod(salary, 1.01))); assertEquivalence(q, jpql); } @@ -473,46 +475,47 @@ public class TestTypesafeCriteria extends CriteriaTest { String jpql = "SELECT e.name, CASE e.rating WHEN 1 THEN 10 " + "WHEN 2 THEN 20 ELSE 30 END " + "FROM Employee e WHERE e.department.name = 'Engineering'"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root e = q.from(Employee.class); - q.where(cb.equal(e.get(Employee_.department).get(Department_.name), - "Engineering")); -// q.select(e.get(Employee_.name), cb.multiselect(Case(e.get(Employee_.rating)) -// .when(1, 10).when(2, 20).otherwise(30)); + Expression rating = e.get(Employee_.rating); + q.where(cb.equal(e.get(Employee_.department).get(Department_.name), "Engineering")); + q.multiselect(e.get(Employee_.name), + cb.selectCase(rating) + .when(1, 10) + .when(2, 20) + .otherwise(30)); assertEquivalence(q, jpql); } public void testLiterals() { - String jpql = "SELECT p FROM Person p where 'Joe' MEMBER OF " + - "p.nickNames"; - CriteriaQuery q = cb.createQuery(); + String jpql = "SELECT p FROM Person p where 'Joe' MEMBER OF p.nickNames"; + CriteriaQuery q = cb.createQuery(Person.class); Root p = q.from(Person.class); -// q.select(p).where( -// cb.isMember(cb.literal("Joe"), p.get(Person_.nickNames))); + q.select(p).where(cb.isMember(cb.literal("Joe"), p.get(Person_.nickNames))); assertEquivalence(q, jpql); } public void testParameters1() { String jpql = "SELECT c FROM Customer c Where c.status = :stat"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Customer.class); Root c = q.from(Customer.class); Parameter param = cb.parameter(Integer.class, "stat"); -// q.select(c).where(cb.equal(c.get(Customer_.status), param)); + q.select(c).where(cb.equal(c.get(Customer_.status), param)); assertEquivalence(q, jpql, new String[] { "stat" }, new Object[] { 1 }); } public void testParameters2() { - String jpql = "SELECT c FROM Customer c Where c.status = :stat AND " - + "c.name = :name"; - CriteriaQuery q = cb.createQuery(); + String jpql = "SELECT c FROM Customer c Where c.status = :stat AND c.name = :name"; + + CriteriaQuery q = cb.createQuery(Customer.class); Root c = q.from(Customer.class); Parameter param1 = cb.parameter(Integer.class, "stat"); Parameter param2 = cb.parameter(String.class, "name"); -// q.select(c).where( -// cb.and(cb.equal(c.get(Customer_.status), param1), cb.equal(c -// .get(Customer_.name), param2))); + q.select(c).where(cb.and(cb.equal(c.get(Customer_.status), param1), + cb.equal(c.get(Customer_.name), param2))); assertEquivalence(q, jpql, new String[] { "stat", "name" }, new Object[] { 1, "test" }); @@ -520,22 +523,24 @@ public class TestTypesafeCriteria extends CriteriaTest { public void testParameters3() { String jpql = "SELECT c FROM Customer c Where c.status = :stat"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Customer.class); Root c = q.from(Customer.class); Parameter param = cb.parameter(Integer.class, "stat"); -// q.select(c).where(cb.equal(c.get(Customer_.status), param)); + q.select(c).where(cb.equal(c.get(Customer_.status), param)); + assertEquivalence(q, jpql, new String[]{"stat"}, new Object[] { 1 }); } public void testParameters4() { - String jpql = "SELECT c FROM Customer c Where c.status = :stat AND " - + "c.name = :name"; - CriteriaQuery q = cb.createQuery(); + String jpql = "SELECT c FROM Customer c Where c.status = :stat AND c.name = :name"; + + CriteriaQuery q = cb.createQuery(Customer.class); Root c = q.from(Customer.class); Parameter param1 = cb.parameter(Integer.class, "stat"); Parameter param2 = cb.parameter(String.class, "name"); -// q.select(c).where(cb.and(cb.equal(c.get(Customer_.status), param1), -// cb.equal(c.get(Customer_.name), param2))); + q.select(c).where(cb.and(cb.equal(c.get(Customer_.status), param1), + cb.equal(c.get(Customer_.name), param2))); assertEquivalence(q, jpql, new String[]{"stat", "name"}, new Object[] { 1, "test" }); } @@ -543,16 +548,16 @@ public class TestTypesafeCriteria extends CriteriaTest { @AllowFailure(message="collection valued parameter does not work in in()") public void testParameters5() { String jpql = "SELECT c FROM Customer c Where c.status IN (:coll)"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createQuery(Customer.class); Root c = q.from(Customer.class); - Parameter param1 = cb.parameter(List.class, "coll"); + ParameterExpression param1 = cb.parameter(List.class, "coll"); q.where(c.get(Customer_.status).in(param1)); - // q.select(c).where(cb.in(c.get(Customer_.status)).value(params1)); + q.select(c); List vals = new ArrayList(); vals.add(1); vals.add(2); - assertEquivalence(q, jpql, new String[] {"coll"}, - new Object[] {vals}); + assertEquivalence(q, jpql, new String[] {"coll"}, new Object[] {vals}); } @AllowFailure(message="Generates invalid SQL") @@ -560,29 +565,27 @@ public class TestTypesafeCriteria extends CriteriaTest { String jpql = "SELECT v.location.street, KEY(i).title, VALUE(i) FROM " + "VideoStore v JOIN v.videoInventory i WHERE v.location.zipCode = " + "'94301' AND VALUE(i) > 0"; - CriteriaQuery q = cb.createQuery(); + + CriteriaQuery q = cb.createTupleQuery(); Root v = q.from(VideoStore.class); - MapJoin inv = v - .join(VideoStore_.videoInventory); + MapJoin i = v.join(VideoStore_.videoInventory); q.where(cb.and( - cb.equal(v.get(VideoStore_.location).get(Address_.zipCode), - "94301"), cb.gt(inv.value(), 0))); -// q.select(v.get(VideoStore_.location).get(Address_.street), inv.key() -// .get(Movie_.title), inv.value()); + cb.equal(v.get(VideoStore_.location).get(Address_.zipCode), "94301"), + cb.gt(i.value(), 0))); + q.multiselect(v.get(VideoStore_.location).get(Address_.street), + i.key().get(Movie_.title), + i.value()); assertEquivalence(q, jpql); } public void testNewConstruct() { - String jpql = "SELECT NEW CustomerDetails(c.id, c.status) FROM " - + "Customer c"; + String jpql = "SELECT NEW CustomerDetails(c.id, c.status) FROM Customer c"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(CustomerDetails.class); Root c = q.from(Customer.class); -// q.select(cb.multiselect(CustomerDetails.class, c.get(Customer_.id), -// c.get(Customer_.status)) -// ); + q.select(cb.construct(CustomerDetails.class, c.get(Customer_.id), c.get(Customer_.status))); assertEquivalence(q, jpql); } @@ -626,13 +629,13 @@ public class TestTypesafeCriteria extends CriteriaTest { + "goodCustomer.balanceOwed < (SELECT AVG(c.balanceOwed) " + " FROM " + "Customer c)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Customer.class); Root goodCustomer = q.from(Customer.class); Subquery sq = q.subquery(Double.class); Root c = sq.from(Customer.class); q.where(cb.lt(goodCustomer.get(Customer_.balanceOwed), sq.select(cb .avg(c.get(Customer_.balanceOwed))))); -// q.select(goodCustomer); + q.select(goodCustomer); assertEquivalence(q, jpql); } @@ -641,14 +644,14 @@ public class TestTypesafeCriteria extends CriteriaTest { String jpql = "SELECT DISTINCT emp FROM Employee emp WHERE EXISTS (" + "SELECT spouseEmp FROM Employee spouseEmp WHERE spouseEmp =" + " emp.spouse)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Employee.class); Root emp = q.from(Employee.class); Subquery sq = q.subquery(Employee.class); Root spouseEmp = sq.from(Employee.class); sq.select(spouseEmp); sq.where(cb.equal(spouseEmp, emp.get(Employee_.spouse))); q.where(cb.exists(sq)); -// q.select(emp).distinct(true); + q.select(emp).distinct(true); assertEquivalence(q, jpql); } @@ -657,9 +660,9 @@ public class TestTypesafeCriteria extends CriteriaTest { String jpql = "SELECT emp FROM Employee emp WHERE emp.salary > ALL (" + "SELECT m.salary FROM Manager m WHERE m.department = " + "emp.department)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Employee.class); Root emp = q.from(Employee.class); -// q.select(emp); + q.select(emp); Subquery sq = q.subquery(BigDecimal.class); Root m = sq.from(Manager.class); sq.select(m.get(Manager_.salary)); @@ -673,9 +676,9 @@ public class TestTypesafeCriteria extends CriteriaTest { public void testSubqueries4() { String jpql = "SELECT c FROM Customer c WHERE " + "(SELECT COUNT(o) FROM c.orders o) > 10"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Customer.class); Root c1 = q.from(Customer.class); -// q.select(c1); + q.select(c1); Subquery sq3 = q.subquery(Long.class); Root c2 = sq3.correlate(c1); Join o = c2.join(Customer_.orders); @@ -687,9 +690,9 @@ public class TestTypesafeCriteria extends CriteriaTest { public void testSubqueries5() { String jpql = "SELECT o FROM Order o WHERE 10000 < ALL (" + "SELECT a.balance FROM o.customer c JOIN c.accounts a)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Order.class); Root o = q.from(Order.class); -// q.select(o); + q.select(o); Subquery sq = q.subquery(Integer.class); Root osq = sq.correlate(o); Join c = osq.join(Order_.customer); @@ -700,13 +703,12 @@ public class TestTypesafeCriteria extends CriteriaTest { assertEquivalence(q, jpql); } - @AllowFailure(message="Root of subquery.delgate is not set") public void testSubqueries6() { String jpql = "SELECT o FROM Order o JOIN o.customer c WHERE 10000 < " + "ALL (SELECT a.balance FROM c.accounts a)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Order.class); Root o = q.from(Order.class); -// q.select(o); + q.select(o); Join c = o.join(Order_.customer); Subquery sq = q.subquery(Integer.class); Join csq = sq.correlate(c); @@ -720,12 +722,12 @@ public class TestTypesafeCriteria extends CriteriaTest { public void testGroupByAndHaving() { String jpql = "SELECT c.status, AVG(c.filledOrderCount), COUNT(c) FROM " + "Customer c GROUP BY c.status HAVING c.status IN (1, 2)"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root c = q.from(Customer.class); q.groupBy(c.get(Customer_.status)); q.having(cb.in(c.get(Customer_.status)).value(1).value(2)); -// q.select(c.get(Customer_.status), cb.avg(c -// .get(Customer_.filledOrderCount)), cb.count(c)); + q.multiselect(c.get(Customer_.status), cb.avg(c + .get(Customer_.filledOrderCount)), cb.count(c)); assertEquivalence(q, jpql); } @@ -734,14 +736,14 @@ public class TestTypesafeCriteria extends CriteriaTest { String jpql = "SELECT o FROM Customer c JOIN c.orders o " + "JOIN c.address a WHERE a.state = 'CA' ORDER BY o.quantity DESC, " + "o.totalCost"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createQuery(Order.class); Root c = q.from(Customer.class); Join o = c.join(Customer_.orders); Join a = c.join(Customer_.address); q.where(cb.equal(a.get(Address_.state), "CA")); q.orderBy(cb.desc(o.get(Order_.quantity)), cb.asc(o .get(Order_.totalCost))); -// q.select(o); + q.select(o); assertEquivalence(q, jpql); } @@ -750,14 +752,14 @@ public class TestTypesafeCriteria extends CriteriaTest { String jpql = "SELECT o.quantity, a.zipCode FROM Customer c " + "JOIN c.orders o JOIN c.address a WHERE a.state = 'CA' " + "ORDER BY o.quantity, a.zipCode"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root c = q.from(Customer.class); Join o = c.join(Customer_.orders); Join a = c.join(Customer_.address); q.where(cb.equal(a.get(Address_.state), "CA")); q.orderBy(cb.asc(o.get(Order_.quantity)), cb.asc(a .get(Address_.zipCode))); -// q.select(o.get(Order_.quantity), a.get(Address_.zipCode)); + q.multiselect(o.get(Order_.quantity), a.get(Address_.zipCode)); assertEquivalence(q, jpql); } @@ -767,29 +769,29 @@ public class TestTypesafeCriteria extends CriteriaTest { + "a.zipCode FROM Customer c JOIN c.orders o JOIN c.address a " + "WHERE a.state = 'CA' AND a.county = 'Santa Clara' " + "ORDER BY o.quantity, taxedCost, a.zipCode"; - CriteriaQuery q = cb.createQuery(); + CriteriaQuery q = cb.createTupleQuery(); Root c = q.from(Customer.class); Join o = c.join(Customer_.orders); Join a = c.join(Customer_.address); - q.where(cb.equal(a.get(Address_.state), "CA"), cb.equal(a - .get(Address_.county), "Santa Clara")); - q.orderBy(cb.asc(o.get(Order_.quantity)), cb.asc(cb.prod( - o.get(Order_.totalCost), 1.08)), + Expression taxedCost = cb.prod(o.get(Order_.totalCost), 1.08); + taxedCost.setAlias("taxedCost"); + q.where(cb.equal(a.get(Address_.state), "CA"), + cb.equal(a.get(Address_.county), "Santa Clara")); + q.orderBy(cb.asc(o.get(Order_.quantity)), + cb.asc(taxedCost), cb.asc(a.get(Address_.zipCode))); -// q.select(o.get(Order_.quantity), cb.prod( -// o.get(Order_.totalCost), 1.08), a.get(Address_.zipCode)); + q.multiselect(o.get(Order_.quantity), taxedCost, a.get(Address_.zipCode)); assertEquivalence(q, jpql); } public void testOrdering4() { - String jpql = "SELECT c FROM Customer c " - + "ORDER BY c.name DESC, c.status"; - CriteriaQuery q = cb.createQuery(); + String jpql = "SELECT c FROM Customer c ORDER BY c.name DESC, c.status"; + CriteriaQuery q = cb.createQuery(Customer.class); Root c = q.from(Customer.class); - q.orderBy(cb.desc(c.get(Customer_.name)), cb.asc(c - .get(Customer_.status))); -// q.select(c); + q.orderBy(cb.desc(c.get(Customer_.name)), + cb.asc(c.get(Customer_.status))); + q.select(c); assertEquivalence(q, jpql); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/ImplicitFieldAccessMappedSuperclass_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/ImplicitFieldAccessMappedSuperclass_.java index c467ab82d..a257bcd5f 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/ImplicitFieldAccessMappedSuperclass_.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/ImplicitFieldAccessMappedSuperclass_.java @@ -12,4 +12,5 @@ import javax.persistence.metamodel.SingularAttribute; public class ImplicitFieldAccessMappedSuperclass_ { public static volatile SingularAttribute createTime; public static volatile SingularAttribute id; + public static volatile SingularAttribute version; } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetamodel.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetamodel.java index c5a706166..c484d198b 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetamodel.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMetamodel.java @@ -35,7 +35,6 @@ import javax.persistence.metamodel.PluralAttribute.CollectionType; import javax.persistence.metamodel.Type.PersistenceType; import org.apache.openjpa.meta.ClassMetaData; -import org.apache.openjpa.meta.MetaDataRepository; import org.apache.openjpa.persistence.enhance.identity.Book; import org.apache.openjpa.persistence.enhance.identity.BookId; import org.apache.openjpa.persistence.enhance.identity.Library; @@ -82,10 +81,9 @@ public class TestMetamodel extends SingleEMFTestCase { } public void testMetaClassFieldsArePopulated() { - EntityType m = - model.entity(ImplicitFieldAccessSubclass.class); - Class mCls = m.getJavaType(); + EntityType m = model.entity(ImplicitFieldAccessSubclass.class); assertNotNull(m); + Class mCls = m.getJavaType(); assertSame(ImplicitFieldAccessSubclass.class, mCls); Class m2Cls = model.repos.getMetaModel(mCls, true); @@ -102,7 +100,7 @@ public class TestMetamodel extends SingleEMFTestCase { } } - public void testDomainClassAreCategorizedInPersistentCategory() { + public void testDomainClassCategorizedInPersistentCategory() { assertCategory(PersistenceType.MAPPED_SUPERCLASS, ImplicitFieldAccessMappedSuperclass.class); assertCategory(PersistenceType.ENTITY, ImplicitFieldAccessBase.class); assertCategory(PersistenceType.ENTITY, ImplicitFieldAccessSubclass.class); @@ -130,19 +128,15 @@ public class TestMetamodel extends SingleEMFTestCase { e0.getSingularAttribute("f0", ExplicitFieldAccess.class); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { - + // good } - ManagedType e1 = model.entity( - ImplicitFieldAccessSubclass.class); + ManagedType e1 = model.entity(ImplicitFieldAccessSubclass.class); assertNotNull(e1.getAttribute("f0")); - } public void testAttributeByDeclaration() { - ManagedType e0 = - model.entity(ImplicitFieldAccessBase.class); - ManagedType e1 = - model.entity(ImplicitFieldAccessSubclass.class); + ManagedType e0 = model.entity(ImplicitFieldAccessBase.class); + ManagedType e1 = model.entity(ImplicitFieldAccessSubclass.class); assertNotNull(e0.getAttribute("f0")); assertNotNull(e1.getAttribute("f0")); System.err.println(e0.getAttribute("f0")); @@ -151,14 +145,13 @@ public class TestMetamodel extends SingleEMFTestCase { assertSame(e0.getAttribute("f0"), e0.getSingularAttribute("f0", String.class)); assertSame(e1.getAttribute("f0"), e1.getSingularAttribute("f0", String.class)); assertNotSame(e0.getAttribute("f0"), e1.getAttribute("f0")); - assertNotSame(e0.getSingularAttribute("f0", String.class), e1.getSingularAttribute("f0", - String.class)); + assertNotSame(e0.getSingularAttribute("f0", String.class), e1.getSingularAttribute("f0", String.class)); assertNotNull(e0.getDeclaredAttribute("f0")); try { e1.getDeclaredAttribute("f0"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { - + // good } } @@ -189,10 +182,8 @@ public class TestMetamodel extends SingleEMFTestCase { } public void testPCSet() { - ManagedType e0 = - model.entity(ImplicitFieldAccessBase.class); - ManagedType r1 = - model.entity(ExplicitFieldAccess.class); + ManagedType e0 = model.entity(ImplicitFieldAccessBase.class); + ManagedType r1 = model.entity(ExplicitFieldAccess.class); SetAttribute relSet = e0.getSet("setRelation", ExplicitFieldAccess.class); assertEquals(javax.persistence.metamodel.PluralAttribute.CollectionType.SET, relSet.getCollectionType()); assertEquals(e0, relSet.getDeclaringType()); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ResultItemImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ResultItemImpl.java index fadbcc387..4777719ce 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ResultItemImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ResultItemImpl.java @@ -18,7 +18,7 @@ */ package org.apache.openjpa.persistence; -import javax.persistence.ResultItem; +import javax.persistence.TupleElement; /** * A single dimension of projection in query result. @@ -27,9 +27,9 @@ import javax.persistence.ResultItem; * * @param type of the result */ -public class ResultItemImpl implements ResultItem { +public class ResultItemImpl implements TupleElement { protected String _alias; - protected Class _cls; + protected final Class _cls; protected ResultItemImpl(Class cls) { _cls = cls; diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java index bc3788d4c..66dd0c831 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java @@ -89,7 +89,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { * @return query object */ public CriteriaQuery createQuery(Class resultClass) { - throw new UnsupportedOperationException(); + return new CriteriaQueryImpl(_model, resultClass); } /** @@ -98,7 +98,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { * @return query object */ public CriteriaQuery createTupleQuery() { - throw new UnsupportedOperationException(); + return new CriteriaQueryImpl(_model, Tuple.class); } public Object parse(String ql, ExpressionStoreQuery query) { @@ -106,22 +106,12 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { } public void populate(Object parsed, ExpressionStoreQuery query) { - CriteriaQueryImpl c = (CriteriaQueryImpl) parsed; + CriteriaQueryImpl c = (CriteriaQueryImpl) parsed; query.invalidateCompilation(); query.getContext().setCandidateType(c.getRoot().getJavaType(), true); query.setQuery(parsed); } - /** - * Define a select list item corresponding to a constructor. - * @param result class whose instance is to be constructed - * @param selections arguments to the constructor - * @return selection item - */ - public Selection construct(Class result, Selection... selections) { - throw new UnsupportedOperationException(); - } - public Expression abs(Expression x) { return new Expressions.Abs(x); } @@ -199,8 +189,8 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Count(x, true); } - public CriteriaQuery createQuery() { - return new CriteriaQueryImpl(_model); + public CriteriaQuery createQuery() { + return new CriteriaQueryImpl(_model, Object.class); } public Expression currentDate() { @@ -305,8 +295,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.In(expression); } - public > Predicate isEmpty( - Expression collection) { + public > Predicate isEmpty(Expression collection) { return new Expressions.IsEmpty(collection); } @@ -314,28 +303,23 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Equal(x, false); } - public > Predicate isMember(E e, - Expression c) { + public > Predicate isMember(E e, Expression c) { return new Expressions.IsMember(e, c); } - public > Predicate isMember(Expression e, - Expression c) { + public > Predicate isMember(Expression e, Expression c) { return new Expressions.IsMember(e.getJavaType(), e, c); } - public > Predicate isNotEmpty( - Expression collection) { - return isEmpty(collection).negate(); + public > Predicate isNotEmpty(Expression collection) { + return new Expressions.IsNotEmpty(collection); } - public > Predicate isNotMember(E e, - Expression c) { + public > Predicate isNotMember(E e, Expression c) { return isMember(e, c).negate(); } - public > Predicate isNotMember( - Expression e, Expression c) { + public > Predicate isNotMember(Expression e, Expression c) { return isMember(e, c).negate(); } @@ -347,8 +331,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { throw new AbstractMethodError(); } - public Predicate le(Expression x, - Expression y) { + public Predicate le(Expression x, Expression y) { return new Expressions.LessThanEqual(x,y); } @@ -365,24 +348,20 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { } - public > Predicate lessThan( - Expression x, Expression y) { + public > Predicate lessThan(Expression x, Expression y) { return new Expressions.LessThan(x,y); } - public > Predicate lessThan( - Expression x, Y y) { + public > Predicate lessThan(Expression x, Y y) { return new Expressions.LessThan(x,y); } - public > Predicate lessThanOrEqualTo( - Expression x, Expression y) { + public > Predicate lessThanOrEqualTo(Expression x, Expression y) { return new Expressions.LessThanEqual(x,y); } - public > Predicate lessThanOrEqualTo( - Expression x, Y y) { + public > Predicate lessThanOrEqualTo(Expression x, Y y) { return new Expressions.LessThanEqual(x,y); } @@ -394,23 +373,19 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Like(x,pattern); } - public Predicate like(Expression x, Expression pattern, - Expression escapeChar) { + public Predicate like(Expression x, Expression pattern, Expression escapeChar) { return new Expressions.Like(x,pattern,escapeChar); } - public Predicate like(Expression x, Expression pattern, - char escapeChar) { + public Predicate like(Expression x, Expression pattern, char escapeChar) { return new Expressions.Like(x,pattern,escapeChar); } - public Predicate like(Expression x, String pattern, - Expression escapeChar) { + public Predicate like(Expression x, String pattern, Expression escapeChar) { return new Expressions.Like(x,pattern,escapeChar); } - public Predicate like(Expression x, String pattern, - char escapeChar) { + public Predicate like(Expression x, String pattern, char escapeChar) { return new Expressions.Like(x,pattern,escapeChar); } @@ -418,8 +393,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Constant(value); } - public Expression locate(Expression x, - Expression pattern) { + public Expression locate(Expression x, Expression pattern) { return new Expressions.Locate(x, pattern); } @@ -428,14 +402,12 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { } - public Expression locate(Expression x, - Expression pattern, Expression from) { + public Expression locate(Expression x, Expression pattern, Expression from) { return new Expressions.Locate(x, pattern, from); } - public Expression locate(Expression x, String pattern, - int from) { + public Expression locate(Expression x, String pattern, int from) { return new Expressions.Locate(x, pattern, from); } @@ -445,8 +417,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { } - public Predicate lt(Expression x, - Expression y) { + public Predicate lt(Expression x, Expression y) { return new Expressions.LessThan(x,y); } @@ -462,8 +433,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Min(x); } - public Expression mod(Expression x, - Expression y) { + public Expression mod(Expression x, Expression y) { return new Expressions.Mod(x,y); } @@ -499,23 +469,19 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return like(x, pattern).negate(); } - public Predicate notLike(Expression x, Expression pattern, - Expression escapeChar) { + public Predicate notLike(Expression x, Expression pattern, Expression escapeChar) { return like(x, pattern, escapeChar).negate(); } - public Predicate notLike(Expression x, Expression pattern, - char escapeChar) { + public Predicate notLike(Expression x, Expression pattern, char escapeChar) { return like(x, pattern, escapeChar).negate(); } - public Predicate notLike(Expression x, String pattern, - Expression escapeChar) { + public Predicate notLike(Expression x, String pattern, Expression escapeChar) { return like(x, pattern, escapeChar).negate(); } - public Predicate notLike(Expression x, String pattern, - char escapeChar) { + public Predicate notLike(Expression x, String pattern, char escapeChar) { return like(x, pattern, escapeChar).negate(); } @@ -546,23 +512,19 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new ParameterImpl(paramClass, name); } - public Expression prod(Expression x, - Expression y) { + public Expression prod(Expression x, Expression y) { return new Expressions.Product(x,y); } - public Expression prod(Expression x, - N y) { + public Expression prod(Expression x, N y) { return new Expressions.Product(x,y); } - public Expression prod(N x, - Expression y) { + public Expression prod(N x, Expression y) { return new Expressions.Product(x,y); } - public Expression quot(Expression x, - Expression y) { + public Expression quot(Expression x, Expression y) { return new Expressions.Quotient(x,y); } @@ -574,17 +536,21 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Quotient(x,y); } - public Selection select(Class result, - Selection... selections) { - return new SelectionImpl(result).setSelections(selections); + /** + * Define a select list item corresponding to a constructor. + * @param result class whose instance is to be constructed + * @param selections arguments to the constructor + * @return selection item + */ + public Selection construct(Class result, Selection... selections) { + return new NewInstanceSelection(result, selections); } public Case selectCase() { return new Expressions.Case(); } - public SimpleCase selectCase( - Expression expression) { + public SimpleCase selectCase(Expression expression) { return new Expressions.SimpleCase(expression); } @@ -592,8 +558,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Size(collection); } - public > Expression size( - Expression collection) { + public > Expression size(Expression collection) { return new Expressions.Size(collection); } @@ -606,8 +571,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Sqrt(x); } - public Expression substring(Expression x, - Expression from) { + public Expression substring(Expression x, Expression from) { return new Expressions.Substring(x, from); } @@ -615,13 +579,11 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Substring(x, from); } - public Expression substring(Expression x, - Expression from, Expression len) { + public Expression substring(Expression x, Expression from, Expression len) { return new Expressions.Substring(x, from, len); } - public Expression substring(Expression x, int from, - int len) { + public Expression substring(Expression x, int from, int len) { return new Expressions.Substring(x, from, len); } @@ -629,28 +591,23 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Sum(x); } - public Expression sum(Expression x, - Expression y) { + public Expression sum(Expression x, Expression y) { return new Expressions.Sum(x,y); } - public Expression sum(Expression x, - N y) { + public Expression sum(Expression x, N y) { return new Expressions.Sum(x,y); } - public Expression sum(N x, - Expression y) { + public Expression sum(N x, Expression y) { return new Expressions.Sum(x,y); } - public Expression toBigDecimal( - Expression number) { + public Expression toBigDecimal(Expression number) { return new Expressions.Cast(number, BigDecimal.class); } - public Expression toBigInteger( - Expression number) { + public Expression toBigInteger(Expression number) { return new Expressions.Cast(number, BigInteger.class); } @@ -682,8 +639,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Trim(x, ts); } - public Expression trim(Expression t, - Expression x) { + public Expression trim(Expression t, Expression x) { return new Expressions.Trim(x, t); } @@ -691,8 +647,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser { return new Expressions.Trim(x, t); } - public Expression trim(Trimspec ts, Expression t, - Expression x) { + public Expression trim(Trimspec ts, Expression t, Expression x) { return new Expressions.Trim(x, t, ts); } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java index 68c3b3541..99f0cfca1 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaExpressionBuilder.java @@ -87,16 +87,14 @@ public class CriteriaExpressionBuilder { Class cls = join.getAttribute().getJavaType(); if (join.getAttribute().isAssociation()) { ClassMetaData meta = metamodel.repos.getMetaData(cls, null, true); - PersistenceType type = metamodel.getPersistenceType(meta); + PersistenceType type = MetamodelImpl.getPersistenceType(meta); if (type == PersistenceType.ENTITY || type == PersistenceType.EMBEDDABLE) metas.add(meta); } } if (root.getFetches() != null) { for (Fetch fetch : root.getFetches()) { - metas.add(metamodel.repos.getMetaData( - fetch.getAttribute().getJavaType(), - null, false)); + metas.add(metamodel.repos.getMetaData(fetch.getAttribute().getJavaType(), null, false)); } } } @@ -140,12 +138,10 @@ public class CriteriaExpressionBuilder { exps.grouping = new Value[groupByCount]; for (int i = 0; i < groupByCount; i++) { Expression groupBy = groups.get(i); - exps.grouping[i] = Expressions.toValue( - (ExpressionImpl)groupBy, factory, model, q);; + exps.grouping[i] = Expressions.toValue((ExpressionImpl)groupBy, factory, model, q);; } - exps.having = having == null ? factory.emptyExpression() - : having.toKernelExpression(factory, model, q); + exps.having = having == null ? factory.emptyExpression() : having.toKernelExpression(factory, model, q); } protected void evalDistinct(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl q) { @@ -170,8 +166,7 @@ public class CriteriaExpressionBuilder { for (Root root : roots) { if (root.getJoins() != null) { for (Join join : root.getJoins()) { - filter = and(factory, ((ExpressionImpl)join) - .toKernelExpression(factory, model, q), filter); + filter = and(factory, ((ExpressionImpl)join).toKernelExpression(factory, model, q), filter); } } ((RootImpl)root).addToContext(factory, model, q); @@ -181,17 +176,17 @@ public class CriteriaExpressionBuilder { List> corrJoins = subQuery.getCorrelatedJoins(); if (corrJoins != null) { for (int i = 0; i < corrJoins.size(); i++) - filter = and(factory, ((ExpressionImpl)corrJoins.get(i)) - .toKernelExpression(factory, model, q), filter); + filter = and(factory, ((ExpressionImpl)corrJoins.get(i)).toKernelExpression(factory, model, q), + filter); } } if (where != null) { - filter = and(factory, where.toKernelExpression - (factory, model, q), filter); + filter = and(factory, where.toKernelExpression(factory, model, q), filter); } - if (filter == null) + if (filter == null) { filter = factory.emptyExpression(); + } exps.filter = filter; } @@ -207,27 +202,23 @@ public class CriteriaExpressionBuilder { exps.projections = new Value[selections.size()]; List projections = new ArrayList(); List aliases = new ArrayList(); - getProjections(exps, selections, projections, aliases, factory, q, - model); - exps.projections = projections.toArray(new Value[0]); - exps.projectionAliases = aliases.toArray(new String[0]); + getProjections(exps, selections, projections, aliases, factory, q, model); + exps.projections = projections.toArray(new Value[projections.size()]); + exps.projectionAliases = aliases.toArray(new String[aliases.size()]); } - private void getProjections(QueryExpressions exps, - List> selections, List projections, List aliases, + private void getProjections(QueryExpressions exps, List> selections, + List projections, List aliases, ExpressionFactory factory, CriteriaQueryImpl q, MetamodelImpl model) { for (Selection s : selections) { - List> sels = ((SelectionImpl)s).getSelections(); - if (sels == null) { - projections.add(((ExpressionImpl)s). - toValue(factory, model, q)); - aliases.add(q.getAlias(s)); - } else { - // this is for constructor expression in the selection + if (s instanceof NewInstanceSelection) { exps.resultClass = s.getJavaType(); - getProjections(exps, sels, projections, aliases, factory, q, - model); - } + getProjections(exps, ((NewInstanceSelection)s).getConstructorArguments(), projections, aliases, + factory, q, model); + } else { + projections.add(((ExpressionImpl)s).toValue(factory, model, q)); + aliases.add(q.getAlias(s)); + } } } @@ -269,10 +260,8 @@ public class CriteriaExpressionBuilder { exps.fetchPaths = oPaths.toArray(new String[oPaths.size()]); } - protected static org.apache.openjpa.kernel.exps.Expression and ( - ExpressionFactory factory, - org.apache.openjpa.kernel.exps.Expression e1, - org.apache.openjpa.kernel.exps.Expression e2) { + protected static org.apache.openjpa.kernel.exps.Expression and (ExpressionFactory factory, + org.apache.openjpa.kernel.exps.Expression e1, org.apache.openjpa.kernel.exps.Expression e2) { return e1 == null ? e2 : e2 == null ? e1 : factory.and(e1, e2); } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java index 0c78692d3..f9c796510 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java @@ -71,6 +71,7 @@ public class CriteriaQueryImpl implements CriteriaQuery, AliasContext { private List> _subqueries; private Boolean _distinct; private SubqueryImpl _delegator; + private final Class _resultClass; // AliasContext private int aliasCount = 0; @@ -85,13 +86,15 @@ public class CriteriaQueryImpl implements CriteriaQuery, AliasContext { // SubqueryContext //private Stack _contexts = null; - public CriteriaQueryImpl(MetamodelImpl model) { + public CriteriaQueryImpl(MetamodelImpl model, Class resultClass) { this._model = model; + this._resultClass = resultClass; _aliases = new HashMap, String>(); } - public CriteriaQueryImpl(MetamodelImpl model, SubqueryImpl delegator) { + public CriteriaQueryImpl(MetamodelImpl model, SubqueryImpl delegator) { this._model = model; + this._resultClass = delegator.getJavaType(); _delegator = delegator; _aliases = getAliases(); } @@ -112,7 +115,7 @@ public class CriteriaQueryImpl implements CriteriaQuery, AliasContext { // return _contexts; //} - public CriteriaQuery distinct(boolean distinct) { + public CriteriaQuery distinct(boolean distinct) { _distinct = distinct; return this; } @@ -171,7 +174,7 @@ public class CriteriaQueryImpl implements CriteriaQuery, AliasContext { * @return the modified query */ public CriteriaQuery multiselect(Selection... selections) { - throw new AbstractMethodError(); + return select(selections); } /** @@ -200,26 +203,26 @@ public class CriteriaQueryImpl implements CriteriaQuery, AliasContext { return _selections; } - public CriteriaQuery groupBy(Expression... grouping) { + public CriteriaQuery groupBy(Expression... grouping) { _groups = new ArrayList>(); for (Expression e : grouping) _groups.add(e); return this; } - public CriteriaQuery having(Expression restriction) { + public CriteriaQuery having(Expression restriction) { _having = new PredicateImpl().add(restriction); return this; } - public CriteriaQuery having(Predicate... restrictions) { + public CriteriaQuery having(Predicate... restrictions) { _having = new PredicateImpl(); for (Predicate p : restrictions) _having.add(p); return this; } - public CriteriaQuery orderBy(Order... o) { + public CriteriaQuery orderBy(Order... o) { _orders = Arrays.asList(o); return this; } @@ -232,7 +235,7 @@ public class CriteriaQueryImpl implements CriteriaQuery, AliasContext { * @return the modified query */ public CriteriaQuery select(Selection selection) { - throw new AbstractMethodError(); + return select(new Selection[]{selection}); } public CriteriaQuery select(Selection... selections) { @@ -348,18 +351,18 @@ public class CriteriaQueryImpl implements CriteriaQuery, AliasContext { // _contexts = contexts; //} - public CriteriaQueryImpl getInnermostParent() { + public CriteriaQueryImpl getInnermostParent() { if (_delegator == null) return this; - AbstractQuery parent = _delegator.getParent(); + AbstractQuery parent = _delegator.getParent(); if (parent instanceof CriteriaQueryImpl) - return (CriteriaQueryImpl)parent; + return (CriteriaQueryImpl)parent; // parent is a SubqueryImpl - return ((SubqueryImpl)parent).getDelegate().getInnermostParent(); + return ((SubqueryImpl)parent).getDelegate().getInnermostParent(); } public Map,String> getAliases() { - CriteriaQueryImpl c = getInnermostParent(); + CriteriaQueryImpl c = getInnermostParent(); if (c._aliases == null) c._aliases = new HashMap, String>(); return c._aliases; diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ExpressionImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ExpressionImpl.java index 01dd0e84e..3a4d41c3b 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ExpressionImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ExpressionImpl.java @@ -27,6 +27,7 @@ import javax.persistence.criteria.QueryBuilder.In; import org.apache.openjpa.kernel.exps.ExpressionFactory; import org.apache.openjpa.kernel.exps.Value; +import org.apache.openjpa.persistence.ResultItemImpl; import org.apache.openjpa.persistence.meta.MetamodelImpl; /** @@ -36,13 +37,14 @@ import org.apache.openjpa.persistence.meta.MetamodelImpl; * * @param the type of the value this expression represents. */ -public class ExpressionImpl extends SelectionImpl +public abstract class ExpressionImpl extends ResultItemImpl implements Expression { - Value toValue(ExpressionFactory factory, MetamodelImpl model, - CriteriaQueryImpl q) { - throw new AbstractMethodError(this.getClass().getName()); - } + abstract Value toValue(ExpressionFactory factory, MetamodelImpl model, + CriteriaQueryImpl q); +// { +// throw new AbstractMethodError(this.getClass().getName()); +// } org.apache.openjpa.kernel.exps.Expression toKernelExpression( ExpressionFactory factory, MetamodelImpl model, @@ -65,7 +67,7 @@ public class ExpressionImpl extends SelectionImpl * @return expression */ public Expression as(Class type) { - throw new AbstractMethodError(); + return new Expressions.CastAs(type, this); } /** diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java index 1c00ba890..5f083bc3e 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java @@ -62,7 +62,7 @@ public class Expressions { * * @param the type of the resultant expression */ - public static class UnaryFunctionalExpression + public abstract static class UnaryFunctionalExpression extends ExpressionImpl { protected ExpressionImpl e; @@ -83,7 +83,7 @@ public class Expressions { * * @param the type of the resultant expression */ - public static class BinarayFunctionalExpression + public abstract static class BinarayFunctionalExpression extends ExpressionImpl{ protected ExpressionImpl e1; protected ExpressionImpl e2; @@ -241,6 +241,7 @@ public class Expressions { return factory.cast(Expressions.toValue(e, factory, model, q), b); } } + public static class Concat extends BinarayFunctionalExpression { public Concat(Expression x, Expression y) { super(String.class, x, y); @@ -758,6 +759,29 @@ public class Expressions { } } + public static class IsNotEmpty extends PredicateImpl { + ExpressionImpl collection; + public IsNotEmpty(Expression collection) { + super(); + this.collection = (ExpressionImpl)collection; + } + + @Override + public PredicateImpl clone() { + return new IsNotEmpty(collection); + } + + @Override + public org.apache.openjpa.kernel.exps.Expression toKernelExpression( + ExpressionFactory factory, MetamodelImpl model, + CriteriaQueryImpl q) { + Value val = Expressions.toValue(collection, factory, model, q); + return (isNegated()) + ? factory.isEmpty(val) : factory.isNotEmpty(val); + } + } + + public static class Index extends UnaryFunctionalExpression { public Index(Joins.List e) { super(Integer.class, e); @@ -1262,5 +1286,21 @@ public class Expressions { return factory.not(super.toKernelExpression(factory, model, q)); } } + + public static class CastAs extends ExpressionImpl { + protected final ExpressionImpl actual; + public CastAs(Class cast, ExpressionImpl actual) { + super(cast); + this.actual = actual; + } + + @Override + public org.apache.openjpa.kernel.exps.Value toValue( + ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl q) { + org.apache.openjpa.kernel.exps.Value e = actual.toValue(factory, model, q); + e.setImplicitType(getJavaType()); + return e; + } + } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/FetchPathImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/FetchPathImpl.java index 26de33a97..018a7c497 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/FetchPathImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/FetchPathImpl.java @@ -47,8 +47,7 @@ public class FetchPathImpl extends PathImpl implements Fetch { this(parent, member, JoinType.INNER); } - FetchPathImpl(FetchParent parent, Members.Member member, - JoinType type) { + FetchPathImpl(FetchParent parent, Members.Member member, JoinType type) { super((PathImpl)parent, member, member.getJavaType()); this.joinType = type; } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java index eb328298b..44a68f63a 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Joins.java @@ -38,6 +38,7 @@ import javax.persistence.metamodel.SetAttribute; import org.apache.openjpa.kernel.exps.ExpressionFactory; import org.apache.openjpa.kernel.exps.Value; import org.apache.openjpa.meta.ClassMetaData; +import org.apache.openjpa.meta.FieldMetaData; import org.apache.openjpa.persistence.meta.Members; import org.apache.openjpa.persistence.meta.MetamodelImpl; import org.apache.openjpa.persistence.meta.Members.Member; @@ -56,7 +57,7 @@ public abstract class Joins { * @param type from which joining * @param type of the attribute being joined */ - public static class SingularJoin extends FromImpl implements Join{ + public static class SingularJoin extends FromImpl implements Join { private final JoinType joinType; private boolean allowNull = false; @@ -448,11 +449,7 @@ public abstract class Joins { MetamodelImpl model, CriteriaQueryImpl c) { org.apache.openjpa.kernel.exps.Value path = toValue(factory, model, c); - ClassMetaData meta = _member.fmd.isElementCollection() - ? _member.fmd.getEmbeddedMetaData() - : _member.fmd.getElement().getDeclaredTypeMetaData(); - - Value var = factory.newBoundVariable(c.getAlias(this), meta.getDescribedType()); + Value var = factory.newBoundVariable(c.getAlias(this), _member.fmd.getElement().getDeclaredType()); org.apache.openjpa.kernel.exps.Expression join = factory.bindValueVariable(var, path); c.registerVariable(this, var, path); return join; diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/NewInstanceSelection.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/NewInstanceSelection.java new file mode 100644 index 000000000..5b01d8a60 --- /dev/null +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/NewInstanceSelection.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.criteria; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.criteria.Selection; + +import org.apache.openjpa.persistence.ResultItemImpl; + +/** + * A selection item that constructs new instance of a user-defined class with arguments specified as other selected + * items. + * + * @author Pinaki Poddar + * + * @param + */ +public class NewInstanceSelection extends ResultItemImpl + implements Selection { + + private List> _args; + + public NewInstanceSelection(Class cls, Selection... selections) { + super(cls); + _args = Arrays.asList(selections); + } + + public List> getConstructorArguments() { + return _args; + } +} diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterImpl.java index fcf50ffa9..c6cebd412 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterImpl.java @@ -36,7 +36,7 @@ import org.apache.openjpa.persistence.meta.MetamodelImpl; * * @param the type of value held by this parameter. */ -public class ParameterImpl extends ExpressionImpl implements ParameterExpression{ +public class ParameterImpl extends ExpressionImpl implements ParameterExpression { private String name; private Integer position; @@ -58,11 +58,9 @@ public class ParameterImpl extends ExpressionImpl implements ParameterExpr } @Override - public Value toValue(ExpressionFactory factory, MetamodelImpl model, - CriteriaQueryImpl q) { + public Value toValue(ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl q) { q.registerParameter(this); - ClassMetaData meta = null; Class clzz = getJavaType(); Object paramKey = getKey(); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java index 2e63a28ae..f67f6e8e0 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java @@ -29,6 +29,7 @@ import javax.persistence.metamodel.MapAttribute; import javax.persistence.metamodel.PluralAttribute; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.Type; +import javax.persistence.metamodel.Type.PersistenceType; import org.apache.openjpa.kernel.exps.ExpressionFactory; import org.apache.openjpa.kernel.exps.Value; @@ -43,8 +44,9 @@ import org.apache.openjpa.persistence.meta.MetamodelImpl; * @param Type referenced by the path */ /** - * Path is an expression often representing a persistent member traversed - * from another (parent) path. + * Path is an expression often representing a persistent attribute traversed from another (parent) path. + * The type of the path is the type of the persistent attribute. + * If the persistent attribute is bindable, then further path can be travesered from this path. * * @author Pinaki Poddar * @author Fay Wang @@ -59,8 +61,7 @@ public class PathImpl extends ExpressionImpl implements Path { protected PathImpl _correlatedPath; /** - * Protected. use by root path which neither represent a member nor has a - * parent. + * Protected constructor use by root path which neither represent a member nor has a parent. */ protected PathImpl(Class cls) { super(cls); @@ -69,12 +70,10 @@ public class PathImpl extends ExpressionImpl implements Path { } /** - * Create a path from the given non-null parent representing the the given - * non-null member. The given class denotes the type expressed by this - * path. + * Create a path from the given non-null parent representing the given non-null member. The given class denotes + * the type expressed by this path. */ - public PathImpl(PathImpl parent, Members.Member member, - Class cls) { + public PathImpl(PathImpl parent, Members.Member member, Class cls) { super(cls); _parent = parent; if (_parent.isEmbedded) { @@ -91,6 +90,9 @@ public class PathImpl extends ExpressionImpl implements Path { * */ public Bindable getModel() { + if (_member instanceof Bindable == false) { + throw new IllegalArgumentException(this + " represents a basic path and not a bindable"); + } return (Bindable)_member; } @@ -101,6 +103,9 @@ public class PathImpl extends ExpressionImpl implements Path { return _parent; } + /** + * Gets the path that originates this traversal. Can be itself if this itself is the origin. + */ public PathImpl getInnermostParentPath() { return (_parent == null) ? this : _parent.getInnermostParentPath(); } @@ -115,13 +120,13 @@ public class PathImpl extends ExpressionImpl implements Path { } protected Members.Member getInnermostMember(PathImpl parent, Members.Member member) { - return member != null ? member : getInnermostMember(parent._parent, - parent._member); + return member != null ? member : getInnermostMember(parent._parent, parent._member); } public void setCorrelatedPath(PathImpl correlatedPath) { _correlatedPath = correlatedPath; } + public PathImpl getCorrelatedPath() { return _correlatedPath; } @@ -130,8 +135,7 @@ public class PathImpl extends ExpressionImpl implements Path { * Convert this path to a kernel path. */ @Override - public Value toValue( - ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl q) { + public Value toValue(ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl q) { if (q.isRegistered(this)) return q.getValue(this); org.apache.openjpa.kernel.exps.Path path = null; @@ -151,8 +155,7 @@ public class PathImpl extends ExpressionImpl implements Path { //path.setSchemaAlias(q.getAlias(_parent)); traversePath(_parent, path, _member.fmd); } else if (_parent != null) { - path = (org.apache.openjpa.kernel.exps.Path) - _parent.toValue(factory, model, q); + path = (org.apache.openjpa.kernel.exps.Path)_parent.toValue(factory, model, q); path.get(_member.fmd, allowNull); } else if (_parent == null) { path = factory.newPath(); @@ -206,48 +209,41 @@ public class PathImpl extends ExpressionImpl implements Path { } /** - * Return the path corresponding to the referenced - * single-valued attribute. - * @param atttribute single-valued attribute - * @return path corresponding to the referenced attribute - */ - /** - * Create a new path with this path as parent. + * Gets a new path that represents the given single-valued attribute from this path. */ public Path get(SingularAttribute attr) { - return new PathImpl(this, (Members.SingularAttributeImpl)attr, - attr.getJavaType()); + return new PathImpl(this, (Members.SingularAttributeImpl)attr, attr.getJavaType()); } /** - * Return the path corresponding to the referenced - * collection-valued attribute. - * @param collection collection-valued attribute - * @return expression corresponding to the referenced attribute + * Gets a new path that represents the given multi-valued attribute from this path. */ public > Expression get(PluralAttribute coll) { - return new PathImpl(this, (Members.Member)coll, coll.getJavaType()); + return new PathImpl(this, (Members.PluralAttributeImpl)coll, coll.getJavaType()); } /** - * Return the path corresponding to the referenced - * map-valued attribute. - * @param map map-valued attribute - * @return expression corresponding to the referenced attribute + * Gets a new path that represents the given map-valued attribute from this path. */ public > Expression get(MapAttribute map) { return new PathImpl(this, (Members.MapAttributeImpl)map, (Class)map.getJavaType()); } + /** + * Gets a new path that represents the attribute of the given name from this path. + * + * @exception IllegalArgumentException if this path represents a basic attribute that is can not be traversed + * further. + */ public Path get(String attName) { - Members.Member next = null; Type type = _member.getType(); - switch (type.getPersistenceType()) { - case BASIC: - throw new RuntimeException(attName + " not navigable from " + this); - default: next = (Members.Member) ((ManagedType)type).getAttribute(attName); + if (type.getPersistenceType() == PersistenceType.BASIC) { + throw new IllegalArgumentException(this + " is a basic path and can not be navigated to " + attName); } - return new PathImpl(this, next, (Class)type.getClass()); + + Members.Member next = (Members.Member) + ((ManagedType)type).getAttribute(attName); + return new PathImpl(this, next, next.getJavaType()); } /** @@ -255,5 +251,5 @@ public class PathImpl extends ExpressionImpl implements Path { */ public Expression> type() { return new Expressions.Type(this); - } +} } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java index 623821686..b85473c20 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java @@ -27,10 +27,9 @@ import javax.persistence.criteria.Predicate; import org.apache.openjpa.kernel.exps.ExpressionFactory; import org.apache.openjpa.persistence.meta.MetamodelImpl; -public class PredicateImpl extends ExpressionImpl -implements Predicate { +public class PredicateImpl extends ExpressionImpl implements Predicate { List> _exps; - BooleanOperator _op; + BooleanOperator _op = BooleanOperator.AND; boolean _negated = false; protected PredicateImpl() { @@ -81,33 +80,36 @@ implements Predicate { clone._exps = new ArrayList>(this._exps); return clone; } - + @Override - org.apache.openjpa.kernel.exps.Expression toKernelExpression( - ExpressionFactory factory, MetamodelImpl model, + org.apache.openjpa.kernel.exps.Value toValue(ExpressionFactory factory, MetamodelImpl model, + CriteriaQueryImpl q) { + throw new AbstractMethodError(); + } + + @Override + org.apache.openjpa.kernel.exps.Expression toKernelExpression(ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl q) { if (_exps == null || _exps.isEmpty()) return factory.emptyExpression(); + if (_exps.size() == 1) - return ((ExpressionImpl)_exps.get(0)) - .toKernelExpression(factory, model, q); + return ((ExpressionImpl)_exps.get(0)).toKernelExpression(factory, model, q); + ExpressionImpl e1 = (ExpressionImpl)_exps.get(0); ExpressionImpl e2 = (ExpressionImpl)_exps.get(1); - org.apache.openjpa.kernel.exps.Expression ke1 = - e1.toKernelExpression(factory, model, q); - org.apache.openjpa.kernel.exps.Expression ke2 = - e2.toKernelExpression(factory, model, q); - org.apache.openjpa.kernel.exps.Expression result = - _op == BooleanOperator.AND + org.apache.openjpa.kernel.exps.Expression ke1 = e1.toKernelExpression(factory, model, q); + org.apache.openjpa.kernel.exps.Expression ke2 = e2.toKernelExpression(factory, model, q); + org.apache.openjpa.kernel.exps.Expression result = _op == BooleanOperator.AND ? factory.and(ke1,ke2) : factory.or(ke1, ke2); - for (int i = 2; i < _exps.size(); i++) { - ExpressionImpl e = (ExpressionImpl)_exps.get(i); - result = _op == BooleanOperator.AND - ? factory.and(result, e.toKernelExpression(factory, model, q)) - : factory.or(result, e.toKernelExpression(factory,model,q)); - } - return _negated ? factory.not(result) : result; + for (int i = 2; i < _exps.size(); i++) { + ExpressionImpl e = (ExpressionImpl)_exps.get(i); + result = _op == BooleanOperator.AND + ? factory.and(result, e.toKernelExpression(factory, model, q)) + : factory.or(result, e.toKernelExpression(factory,model,q)); + } + return _negated ? factory.not(result) : result; } public static class And extends PredicateImpl { diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java index e379313f7..1a99d01a5 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java @@ -41,12 +41,12 @@ public class SelectionImpl extends ResultItemImpl super(cls); } - public SelectionImpl setSelections(Selection... selections) { - _sels = Arrays.asList(selections); - return this; - } - - public List> getSelections() { - return _sels; - } +// public SelectionImpl setSelections(Selection... selections) { +// _sels = Arrays.asList(selections); +// return this; +// } +// +// public List> getSelections() { +// return _sels; +// } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java index d1dc8c6c1..a796305ee 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java @@ -41,7 +41,6 @@ import org.apache.openjpa.kernel.exps.QueryExpressions; import org.apache.openjpa.kernel.exps.Value; import org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder; import org.apache.openjpa.meta.ClassMetaData; -import org.apache.openjpa.meta.FieldMetaData; import org.apache.openjpa.meta.JavaTypes; import org.apache.openjpa.persistence.meta.AbstractManagedType; import org.apache.openjpa.persistence.meta.Members; diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java index 6f28a0ec9..ac2d6a76b 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java @@ -38,6 +38,7 @@ import javax.persistence.metamodel.SetAttribute; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.PluralAttribute.CollectionType; +import org.apache.openjpa.kernel.Filters; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.meta.FieldMetaData; @@ -850,8 +851,7 @@ public abstract class AbstractManagedType extends Types.BaseType * Selects if the attribute type matches the given Java class. * null matches any type. */ - public static final class AttributeTypeFilter implements - Filter> { + public static final class AttributeTypeFilter implements Filter> { private final Class _type; private final boolean _invert; @@ -865,13 +865,13 @@ public abstract class AbstractManagedType extends Types.BaseType } public boolean selects(Attribute attr) { - boolean result = _type == null || attr.getJavaType() == _type; + boolean result = _type == null || Filters.canConvert(attr.getJavaType(), _type, false); return _invert ? !result : result; } public AttributeTypeFilter inverse() { return new AttributeTypeFilter(_type, !_invert); - } + } } public static final class AttributeNameFilter implements diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java index f814348e8..6dc55ecd0 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java @@ -98,8 +98,7 @@ public class AnnotationProcessor6 extends AbstractProcessor { private int generatedSourceVersion = 6; private CompileTimeLogger logger; private boolean addHeader = false; - private static Localizer _loc = - Localizer.forPackage(AnnotationProcessor6.class); + private static Localizer _loc = Localizer.forPackage(AnnotationProcessor6.class); /** * Category of members as per JPA 2.0 type system. @@ -181,13 +180,12 @@ public class AnnotationProcessor6 extends AbstractProcessor { setSourceVersion(); setFileManager(); setNamingPolicy(); - addHeader = "true".equalsIgnoreCase(processingEnv.getOptions() - .get("header")); + addHeader = "true".equalsIgnoreCase(processingEnv.getOptions().get("header")); handler = new SourceAnnotationHandler(processingEnv, logger); } /** - * The entry point for javac compiler. + * The entry point for java compiler. */ @Override public boolean process(Set annos, diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java index f663a0082..7e5bb1262 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java @@ -370,9 +370,10 @@ public class SourceAnnotationHandler */ static class TransientFilter implements InclusiveFilter { public boolean includes(Element obj) { + Set modifiers = obj.getModifiers(); boolean isTransient = isAnnotatedWith(obj, Transient.class) - || obj.getModifiers().contains(Modifier.TRANSIENT); - return !isTransient; + || modifiers.contains(Modifier.TRANSIENT); + return !isTransient && !modifiers.contains(Modifier.STATIC); } }