Return-Path: Delivered-To: apmail-openjpa-commits-archive@www.apache.org Received: (qmail 16463 invoked from network); 21 Jan 2009 23:48:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 21 Jan 2009 23:48:57 -0000 Received: (qmail 71957 invoked by uid 500); 21 Jan 2009 23:48:57 -0000 Delivered-To: apmail-openjpa-commits-archive@openjpa.apache.org Received: (qmail 71942 invoked by uid 500); 21 Jan 2009 23:48:57 -0000 Mailing-List: contact commits-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openjpa.apache.org Delivered-To: mailing list commits@openjpa.apache.org Received: (qmail 71933 invoked by uid 99); 21 Jan 2009 23:48:57 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Jan 2009 15:48:57 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Jan 2009 23:48:54 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id D13CF2388A16; Wed, 21 Jan 2009 15:48:32 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r736493 - in /openjpa/branches/1.0.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ Date: Wed, 21 Jan 2009 23:48:32 -0000 To: commits@openjpa.apache.org From: mikedd@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090121234832.D13CF2388A16@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mikedd Date: Wed Jan 21 15:48:32 2009 New Revision: 736493 URL: http://svn.apache.org/viewvc?rev=736493&view=rev Log: OPENJPA-864 do not remove table aliases for databases which use JoinSyntaxes.SYNTAX_DATABASE (no joins). Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java Modified: openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java Modified: openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java?rev=736493&r1=736492&r2=736493&view=diff ============================================================================== --- openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java (original) +++ openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java Wed Jan 21 15:48:32 2009 @@ -476,43 +476,46 @@ if (_parent.getAliases() == null || _subPath == null) return; - - // resolve aliases for subselect from parent - Set entries = _parent.getAliases().entrySet(); - Iterator it = entries.iterator(); - while (it.hasNext()) { - Map.Entry entry = (Map.Entry) it.next(); - 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(); - it = entries.iterator(); + + // Do not remove aliases for databases that use SYNTAX_DATABASE (oracle) + if(_parent._joinSyntax != JoinSyntaxes.SYNTAX_DATABASE) { + // resolve aliases for subselect from parent + Set entries = _parent.getAliases().entrySet(); + Iterator it = entries.iterator(); while (it.hasNext()) { - Map.Entry entry = (Map.Entry) it.next(); + Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); Integer alias = (Integer) entry.getValue(); if (key.toString().indexOf(_subPath) != -1 || - _parent.findTableAlias(alias) == false) { - _parent.removeAlias(key); + _parent.findTableAlias(alias) == false) { + 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(); + it = entries.iterator(); + while (it.hasNext()) { + Map.Entry entry = (Map.Entry) it.next(); + 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); + } } } } Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java?rev=736493&view=auto ============================================================================== --- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java (added) +++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Dependent.java Wed Jan 21 15:48:32 2009 @@ -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.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java?rev=736493&view=auto ============================================================================== --- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java (added) +++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/DependentId.java Wed Jan 21 15:48:32 2009 @@ -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.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java?rev=736493&view=auto ============================================================================== --- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java (added) +++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Employee.java Wed Jan 21 15:48:32 2009 @@ -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.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java?rev=736493&r1=736492&r2=736493&view=diff ============================================================================== --- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java (original) +++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java Wed Jan 21 15:48:32 2009 @@ -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 @@ 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 @@ 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(); + } }