OPENJPA-864 do not remove table aliases for databases which use JoinSyntaxes.SYNTAX_DATABASE (no joins).

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.x@736493 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2009-01-21 23:48:32 +00:00
parent 4d2c0d09e0
commit 25ce88418e
5 changed files with 293 additions and 32 deletions

View File

@ -477,42 +477,45 @@ public class SelectImpl
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);
}
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}