Search in sources :

Example 1 with Filter

use of org.hibernate.annotations.Filter 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)

Example 2 with Filter

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

the class EntityBinder method bindEntity.

public void bindEntity() {
    persistentClass.setAbstract(annotatedClass.isAbstract());
    persistentClass.setClassName(annotatedClass.getName());
    persistentClass.setJpaEntityName(name);
    //persistentClass.setDynamic(false); //no longer needed with the Entity name refactoring?
    persistentClass.setEntityName(annotatedClass.getName());
    bindDiscriminatorValue();
    persistentClass.setLazy(lazy);
    if (proxyClass != null) {
        persistentClass.setProxyInterfaceName(proxyClass.getName());
    }
    persistentClass.setDynamicInsert(dynamicInsert);
    persistentClass.setDynamicUpdate(dynamicUpdate);
    if (persistentClass instanceof RootClass) {
        RootClass rootClass = (RootClass) persistentClass;
        boolean mutable = true;
        //priority on @Immutable, then @Entity.mutable()
        if (annotatedClass.isAnnotationPresent(Immutable.class)) {
            mutable = false;
        } else {
            org.hibernate.annotations.Entity entityAnn = annotatedClass.getAnnotation(org.hibernate.annotations.Entity.class);
            if (entityAnn != null) {
                mutable = entityAnn.mutable();
            }
        }
        rootClass.setMutable(mutable);
        rootClass.setExplicitPolymorphism(isExplicitPolymorphism(polymorphismType));
        if (StringHelper.isNotEmpty(where))
            rootClass.setWhere(where);
        if (cacheConcurrentStrategy != null) {
            rootClass.setCacheConcurrencyStrategy(cacheConcurrentStrategy);
            rootClass.setCacheRegionName(cacheRegion);
            rootClass.setLazyPropertiesCacheable(cacheLazyProperty);
        }
        rootClass.setNaturalIdCacheRegionName(naturalIdCacheRegion);
        boolean forceDiscriminatorInSelects = forceDiscriminator == null ? context.getBuildingOptions().shouldImplicitlyForceDiscriminatorInSelect() : forceDiscriminator;
        rootClass.setForceDiscriminator(forceDiscriminatorInSelects);
        if (insertableDiscriminator != null) {
            rootClass.setDiscriminatorInsertable(insertableDiscriminator);
        }
    } else {
        if (explicitHibernateEntityAnnotation) {
            LOG.entityAnnotationOnNonRoot(annotatedClass.getName());
        }
        if (annotatedClass.isAnnotationPresent(Immutable.class)) {
            LOG.immutableAnnotationOnNonRoot(annotatedClass.getName());
        }
        if (annotatedClass.isAnnotationPresent(Cacheable.class) || annotatedClass.isAnnotationPresent(Cache.class)) {
            LOG.cacheOrCacheableAnnotationOnNonRoot(annotatedClass.getName());
        }
    }
    persistentClass.setOptimisticLockStyle(getVersioning(optimisticLockType));
    persistentClass.setSelectBeforeUpdate(selectBeforeUpdate);
    //set persister if needed
    Persister persisterAnn = annotatedClass.getAnnotation(Persister.class);
    Class persister = null;
    if (persisterAnn != null) {
        persister = persisterAnn.impl();
    } else {
        org.hibernate.annotations.Entity entityAnn = annotatedClass.getAnnotation(org.hibernate.annotations.Entity.class);
        if (entityAnn != null && !BinderHelper.isEmptyAnnotationValue(entityAnn.persister())) {
            try {
                persister = context.getClassLoaderAccess().classForName(entityAnn.persister());
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Could not find persister class: " + entityAnn.persister(), e);
            }
        }
    }
    if (persister != null) {
        persistentClass.setEntityPersisterClass(persister);
    }
    persistentClass.setBatchSize(batchSize);
    //SQL overriding
    SQLInsert sqlInsert = annotatedClass.getAnnotation(SQLInsert.class);
    SQLUpdate sqlUpdate = annotatedClass.getAnnotation(SQLUpdate.class);
    SQLDelete sqlDelete = annotatedClass.getAnnotation(SQLDelete.class);
    SQLDeleteAll sqlDeleteAll = annotatedClass.getAnnotation(SQLDeleteAll.class);
    Loader loader = annotatedClass.getAnnotation(Loader.class);
    if (sqlInsert != null) {
        persistentClass.setCustomSQLInsert(sqlInsert.sql().trim(), sqlInsert.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlInsert.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlUpdate != null) {
        persistentClass.setCustomSQLUpdate(sqlUpdate.sql(), sqlUpdate.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlUpdate.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlDelete != null) {
        persistentClass.setCustomSQLDelete(sqlDelete.sql(), sqlDelete.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDelete.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlDeleteAll != null) {
        persistentClass.setCustomSQLDelete(sqlDeleteAll.sql(), sqlDeleteAll.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDeleteAll.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (loader != null) {
        persistentClass.setLoaderName(loader.namedQuery());
    }
    final JdbcEnvironment jdbcEnvironment = context.getMetadataCollector().getDatabase().getJdbcEnvironment();
    if (annotatedClass.isAnnotationPresent(Synchronize.class)) {
        Synchronize synchronizedWith = annotatedClass.getAnnotation(Synchronize.class);
        String[] tables = synchronizedWith.value();
        for (String table : tables) {
            persistentClass.addSynchronizedTable(context.getBuildingOptions().getPhysicalNamingStrategy().toPhysicalTableName(jdbcEnvironment.getIdentifierHelper().toIdentifier(table), jdbcEnvironment).render(jdbcEnvironment.getDialect()));
        }
    }
    if (annotatedClass.isAnnotationPresent(Subselect.class)) {
        Subselect subselect = annotatedClass.getAnnotation(Subselect.class);
        this.subselect = subselect.value();
    }
    //tuplizers
    if (annotatedClass.isAnnotationPresent(Tuplizers.class)) {
        for (Tuplizer tuplizer : annotatedClass.getAnnotation(Tuplizers.class).value()) {
            EntityMode mode = EntityMode.parse(tuplizer.entityMode());
            //todo tuplizer.entityModeType
            persistentClass.addTuplizer(mode, tuplizer.impl().getName());
        }
    }
    if (annotatedClass.isAnnotationPresent(Tuplizer.class)) {
        Tuplizer tuplizer = annotatedClass.getAnnotation(Tuplizer.class);
        EntityMode mode = EntityMode.parse(tuplizer.entityMode());
        //todo tuplizer.entityModeType
        persistentClass.addTuplizer(mode, tuplizer.impl().getName());
    }
    for (Filter filter : filters) {
        String filterName = filter.name();
        String cond = filter.condition();
        if (BinderHelper.isEmptyAnnotationValue(cond)) {
            FilterDefinition definition = context.getMetadataCollector().getFilterDefinition(filterName);
            cond = definition == null ? null : definition.getDefaultFilterCondition();
            if (StringHelper.isEmpty(cond)) {
                throw new AnnotationException("no filter condition found for filter " + filterName + " in " + this.name);
            }
        }
        persistentClass.addFilter(filterName, cond, filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
    }
    LOG.debugf("Import with entity name %s", name);
    try {
        context.getMetadataCollector().addImport(name, persistentClass.getEntityName());
        String entityName = persistentClass.getEntityName();
        if (!entityName.equals(name)) {
            context.getMetadataCollector().addImport(entityName, entityName);
        }
    } catch (MappingException me) {
        throw new AnnotationException("Use of the same entity name twice: " + name, me);
    }
    processNamedEntityGraphs();
}
Also used : Cacheable(javax.persistence.Cacheable) Synchronize(org.hibernate.annotations.Synchronize) Loader(org.hibernate.annotations.Loader) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) MappingException(org.hibernate.MappingException) FilterDefinition(org.hibernate.engine.spi.FilterDefinition) AnnotationException(org.hibernate.AnnotationException) SQLInsert(org.hibernate.annotations.SQLInsert) Persister(org.hibernate.annotations.Persister) RootClass(org.hibernate.mapping.RootClass) Tuplizer(org.hibernate.annotations.Tuplizer) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Subselect(org.hibernate.annotations.Subselect) SQLDelete(org.hibernate.annotations.SQLDelete) EntityMode(org.hibernate.EntityMode) Filter(org.hibernate.annotations.Filter) PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) XClass(org.hibernate.annotations.common.reflection.XClass) SQLUpdate(org.hibernate.annotations.SQLUpdate) SQLDeleteAll(org.hibernate.annotations.SQLDeleteAll) Tuplizers(org.hibernate.annotations.Tuplizers) NaturalIdCache(org.hibernate.annotations.NaturalIdCache) Cache(org.hibernate.annotations.Cache)

Example 3 with Filter

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

the class AnnotationBinder method bindFilters.

private static void bindFilters(XAnnotatedElement annotatedElement, EntityBinder entityBinder) {
    Filters filtersAnn = annotatedElement.getAnnotation(Filters.class);
    if (filtersAnn != null) {
        for (Filter filter : filtersAnn.value()) {
            entityBinder.addFilter(filter);
        }
    }
    Filter filterAnn = annotatedElement.getAnnotation(Filter.class);
    if (filterAnn != null) {
        entityBinder.addFilter(filterAnn);
    }
}
Also used : Filters(org.hibernate.annotations.Filters) Filter(org.hibernate.annotations.Filter)

Aggregations

Filter (org.hibernate.annotations.Filter)3 AnnotationException (org.hibernate.AnnotationException)2 Filters (org.hibernate.annotations.Filters)2 Cacheable (javax.persistence.Cacheable)1 EntityMode (org.hibernate.EntityMode)1 MappingException (org.hibernate.MappingException)1 Cache (org.hibernate.annotations.Cache)1 FilterJoinTable (org.hibernate.annotations.FilterJoinTable)1 FilterJoinTables (org.hibernate.annotations.FilterJoinTables)1 Loader (org.hibernate.annotations.Loader)1 NaturalIdCache (org.hibernate.annotations.NaturalIdCache)1 Persister (org.hibernate.annotations.Persister)1 SQLDelete (org.hibernate.annotations.SQLDelete)1 SQLDeleteAll (org.hibernate.annotations.SQLDeleteAll)1 SQLInsert (org.hibernate.annotations.SQLInsert)1 SQLUpdate (org.hibernate.annotations.SQLUpdate)1 Subselect (org.hibernate.annotations.Subselect)1 Synchronize (org.hibernate.annotations.Synchronize)1 Tuplizer (org.hibernate.annotations.Tuplizer)1 Tuplizers (org.hibernate.annotations.Tuplizers)1