HHH-2394 Add support for SqlFragmentAlias.entity element

This commit is contained in:
Rob Worsnop 2012-06-28 13:58:05 -04:00 committed by Strong Liu
parent 82c99ccf6f
commit 5cb8d3a8b3
11 changed files with 82 additions and 50 deletions

View File

@ -38,5 +38,6 @@ import java.lang.annotation.Target;
@Retention(RUNTIME)
public @interface SqlFragmentAlias {
String alias();
String table();
String table() default "";
Class entity() default void.class;
}

View File

@ -41,6 +41,7 @@ import org.hibernate.MappingException;
import org.hibernate.annotations.AnyMetaDef;
import org.hibernate.annotations.AnyMetaDefs;
import org.hibernate.annotations.MetaValue;
import org.hibernate.annotations.SqlFragmentAlias;
import org.hibernate.annotations.common.reflection.XAnnotatedElement;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XPackage;
@ -693,4 +694,24 @@ public class BinderHelper {
String propertyPath = isId ? "" : propertyName;
return mappings.getPropertyAnnotatedWithMapsId(persistentXClass, propertyPath);
}
public static Map<String,String> toAliasTableMap(SqlFragmentAlias[] aliases){
Map<String,String> ret = new HashMap<String,String>();
for (int i = 0; i < aliases.length; i++){
if (StringHelper.isNotEmpty(aliases[i].table())){
ret.put(aliases[i].alias(), aliases[i].table());
}
}
return ret;
}
public static Map<String,String> toAliasEntityMap(SqlFragmentAlias[] aliases){
Map<String,String> ret = new HashMap<String,String>();
for (int i = 0; i < aliases.length; i++){
if (aliases[i].entity() != void.class){
ret.put(aliases[i].alias(), aliases[i].entity().getName());
}
}
return ret;
}
}

View File

@ -24,6 +24,7 @@
package org.hibernate.cfg;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@ -2633,7 +2634,7 @@ public final class HbmBinder {
}
String autoAliasInjectionText = filterElement.attributeValue("autoAliasInjection");
boolean autoAliasInjection = StringHelper.isEmpty(autoAliasInjectionText) ? true : Boolean.parseBoolean(autoAliasInjectionText);
collection.addManyToManyFilter(name, condition, autoAliasInjection, aliasTables);
collection.addManyToManyFilter(name, condition, autoAliasInjection, aliasTables, null);
}
}
@ -3047,7 +3048,7 @@ public final class HbmBinder {
LOG.debugf( "Applying filter [%s] as [%s]", name, condition );
String autoAliasInjectionText = filterElement.attributeValue("autoAliasInjection");
boolean autoAliasInjection = StringHelper.isEmpty(autoAliasInjectionText) ? true : Boolean.parseBoolean(autoAliasInjectionText);
filterable.addFilter(name, condition, autoAliasInjection, aliasTables);
filterable.addFilter(name, condition, autoAliasInjection, aliasTables, null);
}
private static void parseFetchProfile(Element element, Mappings mappings, String containingEntityName) {

View File

@ -23,6 +23,15 @@
*/
package org.hibernate.cfg.annotations;
import static org.hibernate.cfg.BinderHelper.toAliasEntityMap;
import static org.hibernate.cfg.BinderHelper.toAliasTableMap;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.ElementCollection;
@ -35,13 +44,6 @@ import javax.persistence.ManyToMany;
import javax.persistence.MapKey;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.jboss.logging.Logger;
import org.hibernate.AnnotationException;
import org.hibernate.FetchMode;
@ -71,7 +73,6 @@ import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import org.hibernate.annotations.SqlFragmentAlias;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.WhereJoinTable;
import org.hibernate.annotations.common.AssertionFailure;
@ -111,6 +112,7 @@ import org.hibernate.mapping.Property;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.TypeDef;
import org.jboss.logging.Logger;
/**
* Base class for binding different types of collections to Hibernate configuration objects.
@ -812,11 +814,11 @@ public abstract class CollectionBinder {
if ( simpleFilter != null ) {
if ( hasAssociationTable ) {
collection.addManyToManyFilter(simpleFilter.name(), getCondition(simpleFilter), simpleFilter.deduceAliasInjectionPoints(),
toTableAliasMap(simpleFilter.aliases()));
toAliasTableMap(simpleFilter.aliases()), toAliasEntityMap(simpleFilter.aliases()));
}
else {
collection.addFilter(simpleFilter.name(), getCondition(simpleFilter), simpleFilter.deduceAliasInjectionPoints(),
toTableAliasMap(simpleFilter.aliases()));
toAliasTableMap(simpleFilter.aliases()), toAliasEntityMap(simpleFilter.aliases()));
}
}
Filters filters = property.getAnnotation( Filters.class );
@ -824,11 +826,11 @@ public abstract class CollectionBinder {
for (Filter filter : filters.value()) {
if ( hasAssociationTable ) {
collection.addManyToManyFilter( filter.name(), getCondition(filter), filter.deduceAliasInjectionPoints(),
toTableAliasMap(filter.aliases()));
toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
}
else {
collection.addFilter(filter.name(), getCondition(filter), filter.deduceAliasInjectionPoints(),
toTableAliasMap(filter.aliases()));
toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
}
}
}
@ -836,7 +838,8 @@ public abstract class CollectionBinder {
if ( simpleFilterJoinTable != null ) {
if ( hasAssociationTable ) {
collection.addFilter(simpleFilterJoinTable.name(), simpleFilterJoinTable.condition(),
simpleFilterJoinTable.deduceAliasInjectionPoints(), toTableAliasMap(simpleFilterJoinTable.aliases()));
simpleFilterJoinTable.deduceAliasInjectionPoints(),
toAliasTableMap(simpleFilterJoinTable.aliases()), toAliasEntityMap(simpleFilterJoinTable.aliases()));
}
else {
throw new AnnotationException(
@ -850,7 +853,8 @@ public abstract class CollectionBinder {
for (FilterJoinTable filter : filterJoinTables.value()) {
if ( hasAssociationTable ) {
collection.addFilter(filter.name(), filter.condition(),
filter.deduceAliasInjectionPoints(), toTableAliasMap(filter.aliases()));
filter.deduceAliasInjectionPoints(),
toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
}
else {
throw new AnnotationException(
@ -895,13 +899,6 @@ public abstract class CollectionBinder {
// );
// }
}
private static Map<String,String> toTableAliasMap(SqlFragmentAlias[] aliases){
Map<String,String> ret = new HashMap<String,String>();
for (int i = 0; i < aliases.length; i++){
ret.put(aliases[i].alias(), aliases[i].table());
}
return ret;
}
private String getCondition(FilterJoinTable filter) {
//set filtering

View File

@ -23,11 +23,14 @@
*/
package org.hibernate.cfg.annotations;
import static org.hibernate.cfg.BinderHelper.toAliasEntityMap;
import static org.hibernate.cfg.BinderHelper.toAliasTableMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.Access;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
@ -36,8 +39,6 @@ import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.SecondaryTables;
import org.jboss.logging.Logger;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode;
@ -64,7 +65,6 @@ import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.hibernate.annotations.SqlFragmentAlias;
import org.hibernate.annotations.Subselect;
import org.hibernate.annotations.Synchronize;
import org.hibernate.annotations.Tables;
@ -98,6 +98,7 @@ import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.TableOwner;
import org.hibernate.mapping.Value;
import org.jboss.logging.Logger;
/**
@ -387,7 +388,8 @@ public class EntityBinder {
);
}
}
persistentClass.addFilter(filterName, cond, filter.deduceAliasInjectionPoints(), toTableAliasMap(filter.aliases()));
persistentClass.addFilter(filterName, cond, filter.deduceAliasInjectionPoints(),
toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
}
LOG.debugf( "Import with entity name %s", name );
try {
@ -402,14 +404,6 @@ public class EntityBinder {
}
}
private static Map<String,String> toTableAliasMap(SqlFragmentAlias[] aliases){
Map<String,String> ret = new HashMap<String,String>();
for (int i = 0; i < aliases.length; i++){
ret.put(aliases[i].alias(), aliases[i].table());
}
return ret;
}
public void bindDiscriminatorValue() {
if ( StringHelper.isEmpty( discriminatorValue ) ) {
Value discriminator = persistentClass.getDiscriminator();

View File

@ -24,11 +24,13 @@
package org.hibernate.internal;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.persister.entity.Joinable;
/**
*
@ -39,13 +41,15 @@ public class FilterConfiguration {
private final String condition;
private final boolean autoAliasInjection;
private final Map<String, String> aliasTableMap;
private final Map<String, String> aliasEntityMap;
private final PersistentClass persistentClass;
public FilterConfiguration(String name, String condition, boolean autoAliasInjection, Map<String, String> aliasTableMap, PersistentClass persistentClass) {
public FilterConfiguration(String name, String condition, boolean autoAliasInjection, Map<String, String> aliasTableMap, Map<String, String> aliasEntityMap, PersistentClass persistentClass) {
this.name = name;
this.condition = condition;
this.autoAliasInjection = autoAliasInjection;
this.aliasTableMap = aliasTableMap;
this.aliasEntityMap = aliasEntityMap;
this.persistentClass = persistentClass;
}
@ -62,8 +66,9 @@ public class FilterConfiguration {
}
public Map<String, String> getAliasTableMap(SessionFactoryImplementor factory) {
if (!CollectionHelper.isEmpty(aliasTableMap)){
return aliasTableMap;
Map<String,String> mergedAliasTableMap = mergeAliasMaps(factory);
if (!mergedAliasTableMap.isEmpty()){
return mergedAliasTableMap;
} else if (persistentClass != null){
String table = persistentClass.getTable().getQualifiedName(factory.getDialect(),
factory.getSettings().getDefaultCatalogName(),
@ -72,6 +77,19 @@ public class FilterConfiguration {
} else{
return Collections.emptyMap();
}
}
private Map<String,String> mergeAliasMaps(SessionFactoryImplementor factory){
Map<String,String> ret = new HashMap<String, String>();
if (aliasTableMap != null){
ret.putAll(aliasTableMap);
}
if (aliasEntityMap != null){
for (Map.Entry<String, String> entry : aliasEntityMap.entrySet()){
ret.put(entry.getKey(),
Joinable.class.cast(factory.getEntityPersister(entry.getValue())).getTableName());
}
}
return ret;
}
}

View File

@ -520,15 +520,15 @@ public abstract class Collection implements Fetchable, Value, Filterable {
return deleteAllCheckStyle;
}
public void addFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap) {
filters.add(new FilterConfiguration(name, condition, autoAliasInjection, aliasTableMap, null));
public void addFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap, java.util.Map<String,String> aliasEntityMap) {
filters.add(new FilterConfiguration(name, condition, autoAliasInjection, aliasTableMap, aliasEntityMap, null));
}
public java.util.List getFilters() {
return filters;
}
public void addManyToManyFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap) {
manyToManyFilters.add(new FilterConfiguration(name, condition, autoAliasInjection, aliasTableMap, null));
public void addManyToManyFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap, java.util.Map<String,String> aliasEntityMap) {
manyToManyFilters.add(new FilterConfiguration(name, condition, autoAliasInjection, aliasTableMap, aliasEntityMap, null));
}
public java.util.List getManyToManyFilters() {

View File

@ -30,7 +30,7 @@ package org.hibernate.mapping;
* @author Steve Ebersole
*/
public interface Filterable {
public void addFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap);
public void addFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap, java.util.Map<String,String> aliasEntityMap);
public java.util.List getFilters();
}

View File

@ -636,8 +636,8 @@ public abstract class PersistentClass implements Serializable, Filterable, MetaA
return deleteCheckStyle;
}
public void addFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap) {
filters.add(new FilterConfiguration(name, condition, autoAliasInjection, aliasTableMap, this));
public void addFilter(String name, String condition, boolean autoAliasInjection, java.util.Map<String,String> aliasTableMap, java.util.Map<String,String> aliasEntityMap) {
filters.add(new FilterConfiguration(name, condition, autoAliasInjection, aliasTableMap, aliasEntityMap, this));
}
public java.util.List getFilters() {

View File

@ -1088,7 +1088,7 @@ public abstract class AbstractEntityPersister
List<FilterConfiguration> filterDefaultConditions = new ArrayList<FilterConfiguration>();
for ( FilterDefinition filterDefinition : entityBinding.getFilterDefinitions() ) {
filterDefaultConditions.add(new FilterConfiguration(filterDefinition.getFilterName(),
filterDefinition.getDefaultFilterCondition(), true, null, null));
filterDefinition.getDefaultFilterCondition(), true, null, null, null));
}
filterHelper = new FilterHelper( filterDefaultConditions, factory);

View File

@ -30,7 +30,7 @@ public class Club {
@OneToMany(mappedBy="club")
@Filters({
@Filter(name="iqMin", condition="{h}.HUMAN_IQ >= :min", aliases={@SqlFragmentAlias(alias="h", table="ZOOLOGY_HUMAN")}),
@Filter(name="iqMin", condition="{h}.HUMAN_IQ >= :min", aliases={@SqlFragmentAlias(alias="h", entity=Human.class)}),
@Filter(name="pregnantMembers", condition="{m}.IS_PREGNANT=1", aliases={@SqlFragmentAlias(alias="m", table="ZOOLOGY_MAMMAL")})
})
private Set<Human> members = new HashSet<Human>();