You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by rp...@apache.org on 2011/09/18 03:22:17 UTC

svn commit: r1172166 - in /openjpa/branches/1.1.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/

Author: rpalache
Date: Sun Sep 18 01:22:17 2011
New Revision: 1172166

URL: http://svn.apache.org/viewvc?rev=1172166&view=rev
Log:
OPENJPA-864 do not remove table aliases for databases which use JoinSyntaxes.SYNTAX_DATABASE (no joins). Merging Mike's change to 1.1.x

Added:
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java
Modified:
    openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java

Modified: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java?rev=1172166&r1=1172165&r2=1172166&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java (original)
+++ openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java Sun Sep 18 01:22:17 2011
@@ -532,39 +532,41 @@ public class SelectImpl
         
         if (_parent.getAliases() == null || _subPath == null)
             return;
-        
-        // resolve aliases for subselect from parent
-        Set<Map.Entry> entries = _parent.getAliases().entrySet();
-        for (Map.Entry entry : entries) {
-            Object key = entry.getKey();
-            Integer alias = (Integer) entry.getValue();
-            if (key.toString().indexOf(_subPath) != -1 ||
-                _parent.findTableAlias(alias) == false) {
-                if (_aliases == null)
-                    _aliases = new HashMap();
-                _aliases.put(key, alias);
-
-                Object tableString = _parent.getTables().get(alias);
-                if (_tables == null)
-                    _tables = new TreeMap();
-                _tables.put(alias, tableString);
-                
-                _removedAliasFromParent.set(alias.intValue());
-            }
-        }
-        
-        if (_aliases != null) {
-            // aliases moved into subselect should be removed from parent
-            entries = _aliases.entrySet();
+
+        // Do not remove aliases for databases that use SYNTAX_DATABASE (oracle)
+        if(_parent._joinSyntax != JoinSyntaxes.SYNTAX_DATABASE) {
+            // resolve aliases for subselect from parent
+            Set<Map.Entry> entries = _parent.getAliases().entrySet();
             for (Map.Entry entry : entries) {
                 Object key = entry.getKey();
                 Integer alias = (Integer) entry.getValue();
                 if (key.toString().indexOf(_subPath) != -1 ||
                     _parent.findTableAlias(alias) == false) {
-                    _parent.removeAlias(key);
+                    if (_aliases == null)
+                        _aliases = new HashMap();
+                    _aliases.put(key, alias);
 
                     Object tableString = _parent.getTables().get(alias);
-                    _parent.removeTable(alias);
+                    if (_tables == null)
+                        _tables = new TreeMap();
+                    _tables.put(alias, tableString);
+                
+                    _removedAliasFromParent.set(alias.intValue());
+                }
+            }
+        
+            if (_aliases != null) {
+                // aliases moved into subselect should be removed from parent
+                entries = _aliases.entrySet();
+                for (Map.Entry entry : entries) {
+                    Object key = entry.getKey();
+                    Integer alias = (Integer) entry.getValue();
+                    if (key.toString().indexOf(_subPath) != -1 ||
+                        _parent.findTableAlias(alias) == false) {
+                        _parent.removeAlias(key);
+                        Object tableString = _parent.getTables().get(alias);
+                        _parent.removeTable(alias);
+                    }
                 }
             }
         }
@@ -573,12 +575,11 @@ public class SelectImpl
     private boolean findTableAlias(Integer alias) {
         // if alias is defined and referenced, return true.
         String value = "t" + alias.toString() + ".";
-        if (_tableAliases != null) {
+        if (_tableAliases != null)
             if (_tableAliases.containsValue(value))
-                return _tables.containsKey(alias);
+              return _tables.containsKey(alias);
             else
-                return _joins != null;
-        }
+              return _joins != null;
         else
             return true;
     }

Added: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java?rev=1172166&view=auto
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java (added)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java Sun Sep 18 01:22:17 2011
@@ -0,0 +1,76 @@
+/*
+ * 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.query;
+
+import java.util.Date;
+
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+import javax.persistence.OneToOne;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+
+@Entity
+@Table(name="SUBQ_DEPENDENT")
+public class Dependent {
+    @EmbeddedId
+    private DependentId id;
+
+    @OneToOne
+    private Employee emp;
+    
+    @Temporal(TemporalType.TIMESTAMP)
+    private Date endDate;
+    
+    private int curStatusId;
+
+    public int getCurStatusId() {
+        return curStatusId;
+    }
+
+    public void setCurStatusId(int curStatusId) {
+        this.curStatusId = curStatusId;
+    }
+
+    public DependentId getId() {
+        return id;
+    }
+
+    public void setId(DependentId id) {
+        this.id = id;
+    }
+
+    public Employee getEmp() {
+        return emp;
+    }
+
+    public void setEmp(Employee emp) {
+        this.emp = emp;
+    }
+
+    public Date getEndDate() {
+        return endDate;
+    }
+
+    public void setEndDate(Date endDate) {
+        this.endDate = endDate;
+    }
+
+}

Added: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java?rev=1172166&view=auto
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java (added)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java Sun Sep 18 01:22:17 2011
@@ -0,0 +1,88 @@
+/*
+ * 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.query;
+
+import java.util.Date;
+
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class DependentId {
+    private String name;
+    private long empid; 
+    private Date effDate;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public long getEmpid() {
+        return empid;
+    }
+
+    public void setEmpid(long empid) {
+        this.empid = empid;
+    }
+
+    public Date getEffDate() {
+        return effDate;
+    }
+
+    public void setEffDate(Date effDate) {
+        this.effDate = effDate;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((effDate == null) ? 0 : effDate.hashCode());
+        result = prime * result + (int) (empid ^ (empid >>> 32));
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        DependentId other = (DependentId) obj;
+        if (effDate == null) {
+            if (other.effDate != null)
+                return false;
+        } else if (!effDate.equals(other.effDate))
+            return false;
+        if (empid != other.empid)
+            return false;
+        if (name == null) {
+            if (other.name != null)
+                return false;
+        } else if (!name.equals(other.name))
+            return false;
+        return true;
+    }
+}

Added: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java?rev=1172166&view=auto
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java (added)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java Sun Sep 18 01:22:17 2011
@@ -0,0 +1,70 @@
+/*
+ * 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.query;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="SUBQ_EMPLOYEE")
+public class Employee {
+
+    @Id 
+    private long empId;
+    private String name;
+    
+    private long someLong;
+
+    private int statusId; 
+
+    public int getStatusId() {
+        return statusId;
+    }
+
+    public void setStatusId(int statusId) {
+        this.statusId = statusId;
+    }
+
+    public long getEmpId() {
+        return empId;
+    }
+
+    public void setEmpId(long empId) {
+        this.empId = empId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public long getSomeLong() {
+        return someLong;
+    }
+
+    public void setSomeLong(long someLong) {
+        this.someLong = someLong;
+    }
+
+    
+}
\ No newline at end of file

Modified: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java?rev=1172166&r1=1172165&r2=1172166&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java (original)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java Sun Sep 18 01:22:17 2011
@@ -18,9 +18,11 @@
  */
 package org.apache.openjpa.persistence.query;
 
+import java.util.Date;
 import java.util.List;
 
 import javax.persistence.EntityManager;
+import javax.persistence.Query;
 
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
@@ -31,8 +33,9 @@ public class TestSubquery
     extends SingleEMFTestCase {
 
     public void setUp() {
-        setUp(Customer.class, Customer.CustomerKey.class,
-            Order.class, OrderItem.class, CLEAR_TABLES);
+        setUp(Customer.class, Customer.CustomerKey.class, Order.class,
+            OrderItem.class, Employee.class, Dependent.class,
+            DependentId.class, CLEAR_TABLES);
     }
 
     static String[]  querys = new String[] {
@@ -100,4 +103,25 @@ public class TestSubquery
         em.getTransaction().rollback();
         em.close();
     }
+
+    /**
+     * Verify a sub query can contain MAX and additional date comparisons 
+     * without losing the correct alias information. This sort of query 
+     * originally caused problems for DBDictionaries which used DATABASE syntax. 
+     */
+    public void testSubSelectMaxDateRange() {        
+        String query =
+            "SELECT e,d from Employee e, Dependent d "
+                + "WHERE e.empId = :empid "
+                + "AND d.id.empid = (SELECT MAX (e2.empId) FROM Employee e2) "
+                + "AND d.id.effDate > :minDate "
+                + "AND d.id.effDate < :maxDate ";
+        EntityManager em = emf.createEntityManager();
+        Query q = em.createQuery(query);
+        q.setParameter("empid", (long) 101);
+        q.setParameter("minDate", new Date(100));
+        q.setParameter("maxDate", new Date(100000));
+        q.getResultList();
+        em.close();
+    }
 }