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 41afc02df..e9fc5e539 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 @@ -576,7 +576,10 @@ public class SelectImpl if (_parent.getAliases() == null || _subPath == null) return; - + + if (_parent._aliases.size() <= 1) + return; + // resolve aliases for subselect from parent Set entries = _parent.getAliases().entrySet(); for (Map.Entry entry : entries) { diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Magazine.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Magazine.java new file mode 100644 index 000000000..664bae729 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Magazine.java @@ -0,0 +1,89 @@ +/* + * 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.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.FetchType; +import java.sql.Date; + +@Entity +public class Magazine implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name="id") + private int id; + + @Column(name="name") + private String name; + + @Column(name="date_published") + private Date datePublished; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="id_publisher") + private Publisher idPublisher; + + + private static final long serialVersionUID = 1L; + + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public Publisher getIdPublisher() { + return this.idPublisher; + } + + public void setIdPublisher(Publisher idPublisher) { + this.idPublisher = idPublisher; + } + + public Date getDatePublished() { + return datePublished; + } + + public void setDatePublished(Date datePublished) { + this.datePublished = datePublished; + } + + @Override + public String toString() { + return name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Publisher.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Publisher.java new file mode 100644 index 000000000..5700ba5ba --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Publisher.java @@ -0,0 +1,75 @@ +/* + * 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.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.OneToMany; +import java.util.Set; + +@Entity +public class Publisher implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name="id") + private int id; + + @Column(name="name") + private String name; + + @OneToMany(mappedBy="idPublisher") + private Set magazineCollection; + + + private static final long serialVersionUID = 1L; + + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getMagazineCollection() { + return this.magazineCollection; + } + + public void setMagazineCollection(Set magazineCollection) { + this.magazineCollection = magazineCollection; + } + + @Override + public String toString() { + return name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java index 33f4b6d46..0eec39e5f 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java @@ -32,7 +32,8 @@ public class TestSubquery public void setUp() { setUp(Customer.class, Customer.CustomerKey.class, - Order.class, OrderItem.class, CLEAR_TABLES); + Order.class, OrderItem.class, + Magazine.class, Publisher.class, CLEAR_TABLES); } static String[] querys = new String[] { @@ -68,6 +69,16 @@ public class TestSubquery " (select sum(o2.amount) from c.orders o2)", "select o1.oid, c.name from Order o1, Customer c where o1.amount = " + " any(select o2.amount from in(c.orders) o2)", + "SELECT p, m "+ + "FROM Publisher p "+ + "LEFT OUTER JOIN p.magazineCollection m "+ + "WHERE m.id = (SELECT MAX(m2.id) "+ + "FROM Magazine m2 "+ + "WHERE m2.idPublisher.id = p.id "+ + "AND m2.datePublished = "+ + "(SELECT MAX(m3.datePublished) "+ + "FROM Magazine m3 "+ + "WHERE m3.idPublisher.id = p.id)) ", // outstanding problem subqueries: //"select o from Order o where o.amount > (select count(o) from Order o)", //"select o from Order o where o.amount > (select count(o2) from Order o2)",