From e0793ab046753f29cabe98f2b2cbf76427612408 Mon Sep 17 00:00:00 2001 From: Heath Thomann Date: Mon, 21 May 2012 00:02:09 +0000 Subject: [PATCH] OPENJPA-1974: Applied to 2.1.x Rick's changes from trunk (2.2.x) git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.1.x@1340872 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/jdbc/sql/SelectImpl.java | 18 +++---- .../jpql/TestOneToManySubQuery.java | 47 +++++++++++++++++++ .../openjpa/persistence/util/EagerEntity.java | 4 ++ 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/TestOneToManySubQuery.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java index 499ff50ff..f30e09765 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java @@ -155,7 +155,7 @@ public class SelectImpl private Set _eagerKeys = null; // subselect support - private List _subsels = null; + private List _subsels = null; private SelectImpl _parent = null; private String _subPath = null; private boolean _hasSub = false; @@ -2050,7 +2050,7 @@ public class SelectImpl return -1; // not found; create alias - i = aliasSize(null); + i = aliasSize(false, null); // System.out.println("GetTableIndex\t"+ // ((_parent != null) ? "Sub" :"") + // " created alias: "+ @@ -2153,19 +2153,19 @@ public class SelectImpl _tables.put(alias, tableString); } + /** * Calculate total number of aliases. + * + * From 1.2.x */ - private int aliasSize(SelectImpl fromSub) { - int aliases = (_parent == null) ? 0 - : _parent.aliasSize(this); + private int aliasSize(boolean fromParent, SelectImpl fromSub) { + int aliases = (fromParent || _parent == null) ? 0 : _parent.aliasSize(false, this); aliases += (_aliases == null) ? 0 : _aliases.size(); if (_subsels != null) { - SelectImpl sub; - for (int i = 0; i < _subsels.size(); i++) { - sub = (SelectImpl) _subsels.get(i); + for (SelectImpl sub : _subsels) { if (sub != fromSub) - aliases += sub.aliasSize(null); + aliases += sub.aliasSize(true, null); } } return aliases; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/TestOneToManySubQuery.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/TestOneToManySubQuery.java new file mode 100644 index 000000000..04bba7982 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/TestOneToManySubQuery.java @@ -0,0 +1,47 @@ +/* + * 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.jpql; + +import javax.persistence.EntityManager; + +import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.apache.openjpa.persistence.util.EagerEmbed; +import org.apache.openjpa.persistence.util.EagerEmbedRel; +import org.apache.openjpa.persistence.util.EagerEntity; + +public class TestOneToManySubQuery extends SingleEMFTestCase { + + public void setUp() throws Exception { + super.setUp(CLEAR_TABLES, EagerEntity.class, EagerEmbed.class, EagerEmbedRel.class); + } + + public void test() { + EntityManager em = emf.createEntityManager(); + try { + assertEquals(0, em.createQuery( + "SELECT e FROM EagerEntity e WHERE EXISTS (SELECT e1 FROM e.eagerSelf e1 WHERE e1.id = 0)" + + " OR EXISTS (SELECT e1 FROM e.eagerSelf e1 WHERE e1.id = 1)", EagerEntity.class).getResultList() + .size()); + + } finally { + em.close(); + + } + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/EagerEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/EagerEntity.java index 933ae16f1..8cb3b94b5 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/EagerEntity.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/EagerEntity.java @@ -26,6 +26,7 @@ import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; +import javax.persistence.OneToMany; import javax.persistence.Transient; @Entity @@ -45,6 +46,9 @@ public class EagerEntity { @ElementCollection(fetch=FetchType.EAGER) private List eagerEmbedColl; + + @OneToMany(fetch=FetchType.EAGER) + private List eagerSelf; @Transient private String transField;