Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

HHH-6877 Multiple LEFT OUTER JOIN subcriterias filters middle level children #254

Closed
wants to merge 1 commit into from

3 participants

@brmeyer
Collaborator

@gbadner , you originally worked on HHH-2049. Can you review this?

@gbadner
Collaborator

The fix for HHH-2049 should not have been applied. Steve Ebersole explains why in comments for that issue.

Since org.hibernate.Criteria should be considered deprecated
(http://docs.jboss.org/hibernate/core/4.1/devguide/en-US/html_single/#d5e3306), we will not be applying fixes that change functionality.

@gbadner gbadner closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
11 hibernate-core/src/main/java/org/hibernate/impl/CriteriaImpl.java
@@ -426,7 +426,7 @@ private Subcriteria(Criteria parent, String path, String alias, int joinType, Cr
this.parent = parent;
this.joinType = joinType;
this.withClause = withClause;
- this.hasRestriction = withClause != null;
+ this.setHasRestriction( withClause != null );
CriteriaImpl.this.subcriteriaList.add( this );
}
@@ -484,11 +484,18 @@ public Criterion getWithClause() {
public boolean hasRestriction() {
return hasRestriction;
}
+
+ private void setHasRestriction(boolean hasRestriction) {
+ this.hasRestriction = hasRestriction;
+ if(hasRestriction && this.parent instanceof Subcriteria) { // Propagate up
+ ((Subcriteria)this.parent).setHasRestriction(hasRestriction);
+ }
+ }
// Criteria impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Criteria add(Criterion expression) {
- hasRestriction = true;
+ setHasRestriction( true );
CriteriaImpl.this.add(this, expression);
return this;
}
View
11 hibernate-testsuite/src/test/java/org/hibernate/test/criteria/Order.hbm.xml
@@ -17,6 +17,17 @@
</id>
<many-to-one name="order" column="order_id" class="Order" />
<property name="articleId" column="article_id" type="string" />
+ <set name="subLines" cascade="all-delete-orphan" access="field" inverse="true" fetch="select">
+ <key column="order_line_id" />
+ <one-to-many class="SubLine" />
+ </set>
+ </class>
+ <class name="SubLine" table="sub_line">
+ <id name="subId" column="sub_line_id" type="int" unsaved-value="0" access="field" >
+ <generator class="identity" />
+ </id>
+ <many-to-one name="orderLine" column="order_line_id" class="OrderLine" />
+ <property name="subProperty" column="sub_property" type="string" />
</class>
</hibernate-mapping>
View
15 hibernate-testsuite/src/test/java/org/hibernate/test/criteria/OrderLine.java
@@ -23,6 +23,10 @@
*/
package org.hibernate.test.criteria;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
public class OrderLine {
private int lineId = 0;
@@ -52,6 +56,17 @@ public void setArticleId(String articleId) {
this.articleId = articleId;
}
+ private Set<SubLine> subLines = new HashSet<SubLine>();
+
+ public Set<SubLine> getSubLines() {
+ return Collections.unmodifiableSet(subLines);
+ }
+
+ public void addSubLine(SubLine subLine){
+ subLine.setOrderLine(this);
+ this.subLines.add(subLine);
+ }
+
public String toString() {
return "[" + getLineId() + ":" + getArticleId() + "]";
}
View
48 hibernate-testsuite/src/test/java/org/hibernate/test/criteria/OuterJoinCriteriaTest.java
@@ -82,6 +82,43 @@ else if ( order3.getOrderId() == o.getOrderId() ) {
s.close();
}
+ /** Test that left outer joins do not filter children, even if there are intermediate levels without any restrictions */
+ public void testMultiLevelSubcriteriaWithNonNullRestrictions() {
+ Session s = openSession();
+ s.getTransaction().begin();
+
+ Criteria rootCriteria = s.createCriteria( Order.class );
+ Criteria firstLevelSubCriteria = rootCriteria.createCriteria( "orderLines", JoinFragment.LEFT_OUTER_JOIN );
+ Criteria secondLevelSubCriteria = firstLevelSubCriteria.createCriteria( "subLines", JoinFragment.LEFT_OUTER_JOIN );
+ assertNotSame( rootCriteria, firstLevelSubCriteria);
+ assertNotSame( rootCriteria, secondLevelSubCriteria);
+ assertNotSame( firstLevelSubCriteria, secondLevelSubCriteria);
+
+ // Leaving first level sub criteria without restrictions
+ // add restrictions to second level sub criteria
+ assertSame(secondLevelSubCriteria, secondLevelSubCriteria.add( Restrictions.eq( "subProperty", "bbb" ) ) );
+
+ List orders = rootCriteria.list();
+
+ // order1 should be returned because it has subline "bbb"
+ // It should have its full collections
+ assertEquals( 1, orders.size() );
+ Order o = (Order) orders.iterator().next();
+ assertEquals( order1.getOrderId(), o.getOrderId() );
+ assertEquals( order1.getLines().size(), o.getLines().size() );
+ for(Iterator it = order1.getLines().iterator(); it.hasNext(); ) {
+ OrderLine line = (OrderLine) it.next();
+ if("1000".equals(line.getArticleId()))
+ assertEquals( 2, line.getSubLines().size()); // Sub lines have not been filtered
+ else if("3000".equals(line.getArticleId()))
+ assertEquals( 0, line.getSubLines().size()); // There are no sub lines
+ else
+ fail( "unknown line" );
+ }
+ s.getTransaction().commit();
+ s.close();
+ }
+
public void testSubcriteriaWithNonNullRestrictionsAliasToEntityMap() {
Session s = openSession();
s.getTransaction().begin();
@@ -376,6 +413,13 @@ protected void prepareTest() {
OrderLine line = new OrderLine();
line.setArticleId( "1000" );
order1.addLine( line );
+ SubLine subLine = new SubLine();
+ subLine.setSubProperty("aaa");
+ line.addSubLine(subLine);
+ subLine = new SubLine();
+ subLine.setSubProperty("bbb");
+ line.addSubLine(subLine);
+
line = new OrderLine();
line.setArticleId( "3000" );
order1.addLine( line );
@@ -400,7 +444,9 @@ protected void cleanupTest() {
Session s = openSession();
s.getTransaction().begin();
- s.createQuery( "delete from OrderLine" ).executeUpdate();
+ s.createQuery( "delete from SubLine" ).executeUpdate();
+
+ s.createQuery( "delete from OrderLine" ).executeUpdate();
s.createQuery( "delete from Order" ).executeUpdate();
View
58 hibernate-testsuite/src/test/java/org/hibernate/test/criteria/SubLine.java
@@ -0,0 +1,58 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2011, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.test.criteria;
+
+ public class SubLine {
+
+ private int subId = 0;
+
+ private OrderLine orderLine;
+
+ private String subProperty;
+
+
+ public int getSubId() {
+ return subId;
+ }
+
+ public OrderLine getOrderLine() {
+ return orderLine;
+ }
+
+ public String getSubProperty() {
+ return subProperty;
+ }
+
+ public void setOrderLine(OrderLine orderLine) {
+ this.orderLine = orderLine;
+ }
+
+ public void setSubProperty(String subProperty) {
+ this.subProperty = subProperty;
+ }
+
+ public String toString() {
+ return "[" + getSubId() + ":" + getSubProperty() + "]";
+ }
+}
Something went wrong with that request. Please try again.