Search in sources :

Example 1 with FilterJoinTable

use of org.hibernate.annotations.FilterJoinTable in project hibernate-orm by hibernate.

the class CollectionBinder method bindFilters.

private void bindFilters(boolean hasAssociationTable) {
    Filter simpleFilter = property.getAnnotation(Filter.class);
    // if ( StringHelper.isNotEmpty( where ) ) collection.setWhere( where );
    if (simpleFilter != null) {
        if (hasAssociationTable) {
            collection.addManyToManyFilter(simpleFilter.name(), getCondition(simpleFilter), simpleFilter.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilter.aliases()), toAliasEntityMap(simpleFilter.aliases()));
        } else {
            collection.addFilter(simpleFilter.name(), getCondition(simpleFilter), simpleFilter.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilter.aliases()), toAliasEntityMap(simpleFilter.aliases()));
        }
    }
    Filters filters = property.getAnnotation(Filters.class);
    if (filters != null) {
        for (Filter filter : filters.value()) {
            if (hasAssociationTable) {
                collection.addManyToManyFilter(filter.name(), getCondition(filter), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
            } else {
                collection.addFilter(filter.name(), getCondition(filter), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
            }
        }
    }
    FilterJoinTable simpleFilterJoinTable = property.getAnnotation(FilterJoinTable.class);
    if (simpleFilterJoinTable != null) {
        if (hasAssociationTable) {
            collection.addFilter(simpleFilterJoinTable.name(), simpleFilterJoinTable.condition(), simpleFilterJoinTable.deduceAliasInjectionPoints(), toAliasTableMap(simpleFilterJoinTable.aliases()), toAliasEntityMap(simpleFilterJoinTable.aliases()));
        } else {
            throw new AnnotationException("Illegal use of @FilterJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
        }
    }
    FilterJoinTables filterJoinTables = property.getAnnotation(FilterJoinTables.class);
    if (filterJoinTables != null) {
        for (FilterJoinTable filter : filterJoinTables.value()) {
            if (hasAssociationTable) {
                collection.addFilter(filter.name(), filter.condition(), filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
            } else {
                throw new AnnotationException("Illegal use of @FilterJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
            }
        }
    }
    StringBuilder whereBuffer = new StringBuilder();
    if (property.getElementClass() != null) {
        Where whereOnClass = property.getElementClass().getAnnotation(Where.class);
        if (whereOnClass != null) {
            String clause = whereOnClass.clause();
            if (StringHelper.isNotEmpty(clause)) {
                whereBuffer.append(clause);
            }
        }
    }
    Where whereOnCollection = property.getAnnotation(Where.class);
    if (whereOnCollection != null) {
        String clause = whereOnCollection.clause();
        if (StringHelper.isNotEmpty(clause)) {
            if (whereBuffer.length() > 0) {
                whereBuffer.append(' ');
                whereBuffer.append(Junction.Nature.AND.getOperator());
                whereBuffer.append(' ');
            }
            whereBuffer.append(clause);
        }
    }
    if (whereBuffer.length() > 0) {
        String whereClause = whereBuffer.toString();
        if (hasAssociationTable) {
            collection.setManyToManyWhere(whereClause);
        } else {
            collection.setWhere(whereClause);
        }
    }
    WhereJoinTable whereJoinTable = property.getAnnotation(WhereJoinTable.class);
    String whereJoinTableClause = whereJoinTable == null ? null : whereJoinTable.clause();
    if (StringHelper.isNotEmpty(whereJoinTableClause)) {
        if (hasAssociationTable) {
            collection.setWhere(whereJoinTableClause);
        } else {
            throw new AnnotationException("Illegal use of @WhereJoinTable on an association without join table:" + StringHelper.qualify(propertyHolder.getPath(), propertyName));
        }
    }
// This cannot happen in annotations since the second fetch is hardcoded to join
// if ( ( ! collection.getManyToManyFilterMap().isEmpty() || collection.getManyToManyWhere() != null ) &&
// collection.getFetchMode() == FetchMode.JOIN &&
// collection.getElement().getFetchMode() != FetchMode.JOIN ) {
// throw new MappingException(
// "association with join table  defining filter or where without join fetching " +
// "not valid within collection using join fetching [" + collection.getRole() + "]"
// );
// }
}
Also used : WhereJoinTable(org.hibernate.annotations.WhereJoinTable) Filters(org.hibernate.annotations.Filters) Filter(org.hibernate.annotations.Filter) AnnotationException(org.hibernate.AnnotationException) FilterJoinTable(org.hibernate.annotations.FilterJoinTable) FilterJoinTables(org.hibernate.annotations.FilterJoinTables) Where(org.hibernate.annotations.Where)

Aggregations

AnnotationException (org.hibernate.AnnotationException)1 Filter (org.hibernate.annotations.Filter)1 FilterJoinTable (org.hibernate.annotations.FilterJoinTable)1 FilterJoinTables (org.hibernate.annotations.FilterJoinTables)1 Filters (org.hibernate.annotations.Filters)1 Where (org.hibernate.annotations.Where)1 WhereJoinTable (org.hibernate.annotations.WhereJoinTable)1