mirror of
https://github.com/apache/openjpa.git
synced 2025-02-08 19:15:21 +00:00
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:
parent
4d2c0d09e0
commit
25ce88418e
@ -477,42 +477,45 @@ public class SelectImpl
|
|||||||
if (_parent.getAliases() == null || _subPath == null)
|
if (_parent.getAliases() == null || _subPath == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// resolve aliases for subselect from parent
|
// Do not remove aliases for databases that use SYNTAX_DATABASE (oracle)
|
||||||
Set entries = _parent.getAliases().entrySet();
|
if(_parent._joinSyntax != JoinSyntaxes.SYNTAX_DATABASE) {
|
||||||
Iterator it = entries.iterator();
|
// resolve aliases for subselect from parent
|
||||||
while (it.hasNext()) {
|
Set entries = _parent.getAliases().entrySet();
|
||||||
Map.Entry entry = (Map.Entry) it.next();
|
Iterator it = entries.iterator();
|
||||||
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();
|
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
Map.Entry entry = (Map.Entry) it.next();
|
Map.Entry entry = (Map.Entry) it.next();
|
||||||
Object key = entry.getKey();
|
Object key = entry.getKey();
|
||||||
Integer alias = (Integer) entry.getValue();
|
Integer alias = (Integer) entry.getValue();
|
||||||
if (key.toString().indexOf(_subPath) != -1 ||
|
if (key.toString().indexOf(_subPath) != -1 ||
|
||||||
_parent.findTableAlias(alias) == false) {
|
_parent.findTableAlias(alias) == false) {
|
||||||
_parent.removeAlias(key);
|
if (_aliases == null)
|
||||||
|
_aliases = new HashMap();
|
||||||
|
_aliases.put(key, alias);
|
||||||
|
|
||||||
Object tableString = _parent.getTables().get(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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -18,9 +18,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.openjpa.persistence.query;
|
package org.apache.openjpa.persistence.query;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.Query;
|
||||||
|
|
||||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||||
|
|
||||||
@ -31,8 +33,9 @@ public class TestSubquery
|
|||||||
extends SingleEMFTestCase {
|
extends SingleEMFTestCase {
|
||||||
|
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
setUp(Customer.class, Customer.CustomerKey.class,
|
setUp(Customer.class, Customer.CustomerKey.class, Order.class,
|
||||||
Order.class, OrderItem.class, CLEAR_TABLES);
|
OrderItem.class, Employee.class, Dependent.class,
|
||||||
|
DependentId.class, CLEAR_TABLES);
|
||||||
}
|
}
|
||||||
|
|
||||||
static String[] querys = new String[] {
|
static String[] querys = new String[] {
|
||||||
@ -100,4 +103,25 @@ public class TestSubquery
|
|||||||
em.getTransaction().rollback();
|
em.getTransaction().rollback();
|
||||||
em.close();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user