From ee01d806772690633694baea5909d69d83f7d6bd Mon Sep 17 00:00:00 2001 From: Rob Worsnop Date: Fri, 18 May 2012 10:00:59 -0400 Subject: [PATCH] HHH-2394 Prefixes won't work; need table set explicitly --- .../org/hibernate/annotations/Filter.java | 3 ++- .../java/org/hibernate/cfg/HbmBinder.java | 6 +++-- .../cfg/annotations/CollectionBinder.java | 16 +++++++++----- .../cfg/annotations/EntityBinder.java | 2 +- .../org/hibernate/mapping/Collection.java | 22 ++++--------------- .../org/hibernate/mapping/Filterable.java | 2 +- .../hibernate/mapping/PersistentClass.java | 3 +-- .../org/hibernate/hibernate-mapping-4.0.xsd | 1 + 8 files changed, 24 insertions(+), 31 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/Filter.java b/hibernate-core/src/main/java/org/hibernate/annotations/Filter.java index 167588e096..ae74009254 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/Filter.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/Filter.java @@ -36,11 +36,12 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; * @author Emmanuel Bernard * @author Matthew Inger * @author Magnus Sandberg + * @author Rob Worsnop */ @Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME) public @interface Filter { String name(); - + String table() default ""; String condition() default ""; } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java index a699d71edf..55f98cc4e3 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java @@ -2614,6 +2614,7 @@ public final class HbmBinder { while ( filters.hasNext() ) { final Element filterElement = ( Element ) filters.next(); final String name = filterElement.attributeValue( "name" ); + final String tableName = filterElement.attributeValue("table"); String condition = filterElement.getTextTrim(); if ( StringHelper.isEmpty(condition) ) condition = filterElement.attributeValue( "condition" ); if ( StringHelper.isEmpty(condition) ) { @@ -2625,7 +2626,7 @@ public final class HbmBinder { if ( LOG.isDebugEnabled() ) { LOG.debugf( "Applying many-to-many filter [%s] as [%s] to role [%s]", name, condition, collection.getRole() ); } - collection.addManyToManyFilter( name, condition ); + collection.addManyToManyFilter( name, tableName, condition ); } } @@ -3014,6 +3015,7 @@ public final class HbmBinder { private static void parseFilter(Element filterElement, Filterable filterable, Mappings model) { final String name = filterElement.attributeValue( "name" ); + final String tableName = filterElement.attributeValue("table"); String condition = filterElement.getTextTrim(); if ( StringHelper.isEmpty(condition) ) { condition = filterElement.attributeValue( "condition" ); @@ -3031,7 +3033,7 @@ public final class HbmBinder { throw new MappingException("no filter condition found for filter: " + name); } LOG.debugf( "Applying filter [%s] as [%s]", name, condition ); - filterable.addFilter( name, condition ); + filterable.addFilter( name, tableName, condition ); } private static void parseFetchProfile(Element element, Mappings mappings, String containingEntityName) { diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index 47bf034328..b62e923f35 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -810,27 +810,27 @@ public abstract class CollectionBinder { //if ( StringHelper.isNotEmpty( where ) ) collection.setWhere( where ); if ( simpleFilter != null ) { if ( hasAssociationTable ) { - collection.addManyToManyFilter( simpleFilter.name(), getCondition( simpleFilter ) ); + collection.addManyToManyFilter( simpleFilter.name(), getTableName(simpleFilter), getCondition( simpleFilter ) ); } else { - collection.addFilter( simpleFilter.name(), getCondition( simpleFilter ) ); + collection.addFilter( simpleFilter.name(), getTableName(simpleFilter), getCondition( simpleFilter ) ); } } Filters filters = property.getAnnotation( Filters.class ); if ( filters != null ) { for (Filter filter : filters.value()) { if ( hasAssociationTable ) { - collection.addManyToManyFilter( filter.name(), getCondition( filter ) ); + collection.addManyToManyFilter( filter.name(), getTableName(simpleFilter), getCondition( filter ) ); } else { - collection.addFilter( filter.name(), getCondition( filter ) ); + collection.addFilter( filter.name(), getTableName(simpleFilter), getCondition( filter ) ); } } } FilterJoinTable simpleFilterJoinTable = property.getAnnotation( FilterJoinTable.class ); if ( simpleFilterJoinTable != null ) { if ( hasAssociationTable ) { - collection.addFilter( simpleFilterJoinTable.name(), getCondition( simpleFilterJoinTable ) ); + collection.addFilter( simpleFilterJoinTable.name(), null, getCondition( simpleFilterJoinTable ) ); } else { throw new AnnotationException( @@ -843,7 +843,7 @@ public abstract class CollectionBinder { if ( filterJoinTables != null ) { for (FilterJoinTable filter : filterJoinTables.value()) { if ( hasAssociationTable ) { - collection.addFilter( filter.name(), getCondition( filter ) ); + collection.addFilter( filter.name(), null, getCondition( filter ) ); } else { throw new AnnotationException( @@ -895,6 +895,10 @@ public abstract class CollectionBinder { String cond = filter.condition(); return getCondition( cond, name ); } + + private String getTableName(Filter filter){ + return BinderHelper.isEmptyAnnotationValue(filter.table())? null : filter.table(); + } private String getCondition(Filter filter) { //set filtering diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index 012e6b912c..20a7676f0f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -384,7 +384,7 @@ public class EntityBinder { ); } } - persistentClass.addFilter( filterName, cond ); + persistentClass.addFilter( filterName, null, cond ); } } else if ( filters.size() > 0 ) { diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Collection.java b/hibernate-core/src/main/java/org/hibernate/mapping/Collection.java index 91880f75d1..64b811b84c 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Collection.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Collection.java @@ -24,7 +24,6 @@ package org.hibernate.mapping; import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Properties; @@ -34,7 +33,6 @@ import org.hibernate.MappingException; import org.hibernate.cfg.Mappings; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.engine.spi.Mapping; -import org.hibernate.internal.FilterConfiguration; import org.hibernate.internal.QualifiedTableNameFilterConfiguration; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.collections.ArrayHelper; @@ -522,29 +520,17 @@ public abstract class Collection implements Fetchable, Value, Filterable { return deleteAllCheckStyle; } - public void addFilter(String name, String condition) { - filters.add(toFilterConfiguration(name, condition)); + public void addFilter(String name, String tableName, String condition) { + filters.add(new QualifiedTableNameFilterConfiguration(name, tableName, condition)); } public java.util.List getFilters() { return filters; } - public void addManyToManyFilter(String name, String condition) { - manyToManyFilters.add(toFilterConfiguration(name, condition)); + public void addManyToManyFilter(String name, String tableName, String condition) { + manyToManyFilters.add(new QualifiedTableNameFilterConfiguration(name, tableName, condition)); } - - private static FilterConfiguration toFilterConfiguration(String name, String condition){ - String tableName = null; - String actualCondition = condition; - int pos = condition.lastIndexOf('.'); - if (pos > -1){ - tableName = condition.substring(0, pos); - actualCondition = condition.substring(pos+1); - } - return new QualifiedTableNameFilterConfiguration(name, tableName, actualCondition); - } - public java.util.List getManyToManyFilters() { return manyToManyFilters; diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Filterable.java b/hibernate-core/src/main/java/org/hibernate/mapping/Filterable.java index 5552937414..25783bace9 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Filterable.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Filterable.java @@ -30,7 +30,7 @@ package org.hibernate.mapping; * @author Steve Ebersole */ public interface Filterable { - public void addFilter(String name, String condition); + public void addFilter(String name, String tableName, String condition); public java.util.List getFilters(); } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java index a92ba5fbd9..c09de99f59 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java @@ -35,7 +35,6 @@ import org.hibernate.MappingException; import org.hibernate.dialect.Dialect; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.engine.spi.Mapping; -import org.hibernate.internal.FilterConfiguration; import org.hibernate.internal.PersistentClassFilterConfiguration; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.StringHelper; @@ -637,7 +636,7 @@ public abstract class PersistentClass implements Serializable, Filterable, MetaA return deleteCheckStyle; } - public void addFilter(String name, String condition) { + public void addFilter(String name, String tableName, String condition) { filters.add(new PersistentClassFilterConfiguration(name, this, condition)); } diff --git a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd index 4f26e29b4c..8be7d1fce8 100644 --- a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd +++ b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd @@ -677,6 +677,7 @@ arbitrary number of queries, and import declarations of arbitrary classes. +