OPENJPA-2289: Applied Albert's patch to trunk

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1415702 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Heath Thomann 2012-11-30 15:39:17 +00:00
parent 9fa9ef4fe8
commit 2d5ab5c862
6 changed files with 257 additions and 1 deletions

View File

@ -1878,6 +1878,10 @@ public class DBDictionary
throw new IllegalArgumentException(syntax); throw new IllegalArgumentException(syntax);
} }
public boolean isImplicitJoin() {
return false;
}
/** /**
* Return a SQL string to act as a placeholder for the given column. * Return a SQL string to act as a placeholder for the given column.
*/ */

View File

@ -1366,6 +1366,10 @@ public class OracleDictionary
} }
} }
public boolean isImplicitJoin() {
return joinSyntax == SYNTAX_DATABASE;
}
/** /**
* Oracle requires special handling of XML column. * Oracle requires special handling of XML column.
* Unless the value length is less or equal to 4000 bytes, * Unless the value length is less or equal to 4000 bytes,

View File

@ -2847,7 +2847,11 @@ public class SelectImpl
|| _sel._from != null) { || _sel._from != null) {
// don't make any joins, but update the path if a variable // don't make any joins, but update the path if a variable
// has been set // has been set
if (this.var != null) {
this.append(this.var); this.append(this.var);
} else if (this.path == null && this.correlatedVar != null && _sel._dict.isImplicitJoin()) {
this.append(this.correlatedVar);
}
this.var = null; this.var = null;
_outer = false; _outer = false;
return this; return this;

View File

@ -0,0 +1,69 @@
/*
* 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.jdbc.query.sub;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "OPENJPA_MAXQUERY_ENTITY")
public class MaxQueryEntity {
public MaxQueryEntity() {
}
public MaxQueryEntity(int id, int domainId, int revision) {
this.id = id;
this.domainId = domainId;
this.revision = revision;
}
@Id
private int id;
private int domainId;
private int revision;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getRevision() {
return revision;
}
public void setRevision(int revision) {
this.revision = revision;
}
public int getDomainId() {
return domainId;
}
public void setDomainId(int domainId) {
this.domainId = domainId;
}
}

View File

@ -0,0 +1,83 @@
/*
* 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.jdbc.query.sub;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "OPENJPA_MAXQUERY_MAPENTITY")
public class MaxQueryMapEntity {
public MaxQueryMapEntity(int id, int revision, String selectCriteria) {
this.id = id;
this.revision = revision;
this.selectCriteria = selectCriteria;
}
public MaxQueryMapEntity() {
}
@Id
private int id;
private int revision;
@ManyToOne
@JoinColumn(name = "refEntity")
private MaxQueryEntity refEntity;
private String selectCriteria;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getRevision() {
return revision;
}
public void setRevision(int revision) {
this.revision = revision;
}
public MaxQueryEntity getRefEntity() {
return refEntity;
}
public void setRefEntity(MaxQueryEntity refEntity) {
this.refEntity = refEntity;
}
public String getSelectCriteria() {
return selectCriteria;
}
public void setSelectCriteria(String selectCriteria) {
this.selectCriteria = selectCriteria;
}
}

View File

@ -0,0 +1,92 @@
/*
* 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.jdbc.query.sub;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestSubQuery extends SingleEMFTestCase {
public void setUp() throws Exception {
super.setUp(CLEAR_TABLES, MaxQueryEntity.class, MaxQueryMapEntity.class);
populate();
}
public void populate() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
MaxQueryEntity mqe = new MaxQueryEntity(1, 1, 1);
MaxQueryMapEntity mqme = new MaxQueryMapEntity(1, 1, "A1");
MaxQueryMapEntity mqme2 = new MaxQueryMapEntity(2, 2, "A2");
mqme.setRefEntity(mqe);
mqme2.setRefEntity(mqe);
em.persist(mqe);
em.persist(mqme);
em.persist(mqme2);
mqe = new MaxQueryEntity(2, 2, 1);
mqme = new MaxQueryMapEntity(3, 1, "B1");
mqme.setRefEntity(mqe);
em.persist(mqe);
em.persist(mqme);
mqme = new MaxQueryMapEntity(4, 2, "B2");
mqme.setRefEntity(mqe);
em.persist(mqme);
mqme = new MaxQueryMapEntity(5, 3, "B3");
mqme.setRefEntity(mqe);
em.persist(mqme);
mqe = new MaxQueryEntity(3, 3, 1);
mqme = new MaxQueryMapEntity(6, 4, "C1");
mqme.setRefEntity(mqe);
em.persist(mqe);
em.persist(mqme);
tran.commit();
em.close();
}
public void test() {
EntityManager em = emf.createEntityManager();
Query query =
em
.createQuery("SELECT e FROM MaxQueryEntity e, MaxQueryMapEntity map "
+ "WHERE "
// + " map.selectCriteria = 'B3' "
// + " AND map.refEntity = e "
// + " AND e.revision = ( SELECT MAX(e_.revision)"
// + " FROM MaxQueryEntity e_"
// + " WHERE e_.domainId = e.domainId )"
// + " AND "
+ " map.revision = ( SELECT MAX(map_.revision)"
+ " FROM MaxQueryMapEntity map_"
+ " WHERE map_.refEntity = map.refEntity )"
);
// assertEquals(1, query.getResultList().size());
assertEquals(9, query.getResultList().size());
em.close();
}
}