org.hibernate.Filter Java Examples

The following examples show how to use org.hibernate.Filter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FilterImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Set the named parameter's value list for this filter.  Used
 * in conjunction with IN-style filter criteria.
 *
 * @param name   The parameter's name.
 * @param values The values to be expanded into an SQL IN list.
 * @return This FilterImpl instance (for method chaining).
 */
public Filter setParameterList(String name, Collection values) throws HibernateException  {
	// Make sure this is a defined parameter and check the incoming value type
	if ( values == null ) {
		throw new IllegalArgumentException( "Collection must be not null!" );
	}
	Type type = definition.getParameterType( name );
	if ( type == null ) {
		throw new HibernateException( "Undefined filter parameter [" + name + "]" );
	}
	if ( values.size() > 0 ) {
		Class elementClass = values.iterator().next().getClass();
		if ( !type.getReturnedClass().isAssignableFrom( elementClass ) ) {
			throw new HibernateException( "Incorrect type for parameter [" + name + "]" );
		}
	}
	parameters.put( name, values );
	return this;
}
 
Example #2
Source File: QueryPlanCache.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the query plan for the given HQL query, creating it and caching it if not already cached
 *
 * @param queryString The HQL query string
 * @param shallow Whether the execution will be shallow
 * @param enabledFilters The filters enabled on the Session
 *
 * @return The query plan
 *
 * @throws QueryException Indicates a problem translating the query
 * @throws MappingException Indicates a problem translating the query
 */
@SuppressWarnings("unchecked")
public HQLQueryPlan getHQLQueryPlan(String queryString, boolean shallow, Map<String,Filter> enabledFilters)
		throws QueryException, MappingException {
	final HQLQueryPlanKey key = new HQLQueryPlanKey( queryString, shallow, enabledFilters );
	HQLQueryPlan value = (HQLQueryPlan) queryPlanCache.get( key );
	if ( value == null ) {
		LOG.tracev( "Unable to locate HQL query plan in cache; generating ({0})", queryString );
		value = new HQLQueryPlan( queryString, shallow, enabledFilters, factory );
		queryPlanCache.putIfAbsent( key, value );
	}
	else {
		LOG.tracev( "Located HQL query plan in cache ({0})", queryString );
	}
	return value;
}
 
Example #3
Source File: QueryPlanCache.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the query plan for the given collection HQL filter fragment, creating it and caching it if not already cached
 *
 * @param filterString The HQL filter fragment
 * @param collectionRole The collection being filtered
 * @param shallow Whether the execution will be shallow
 * @param enabledFilters The filters enabled on the Session
 *
 * @return The query plan
 *
 * @throws QueryException Indicates a problem translating the query
 * @throws MappingException Indicates a problem translating the query
 */
@SuppressWarnings("unchecked")
public FilterQueryPlan getFilterQueryPlan(
		String filterString,
		String collectionRole,
		boolean shallow,
		Map<String,Filter> enabledFilters) throws QueryException, MappingException {
	final FilterQueryPlanKey key =  new FilterQueryPlanKey( filterString, collectionRole, shallow, enabledFilters );
	FilterQueryPlan value = (FilterQueryPlan) queryPlanCache.get( key );
	if ( value == null ) {
		LOG.tracev(
				"Unable to locate collection-filter query plan in cache; generating ({0} : {1} )",
				collectionRole,
				filterString
		);
		value = new FilterQueryPlan( filterString, collectionRole, shallow, enabledFilters,factory );
		queryPlanCache.putIfAbsent( key, value );
	}
	else {
		LOG.tracev( "Located collection-filter query plan in cache ({0} : {1})", collectionRole, filterString );
	}
	return value;
}
 
Example #4
Source File: FilterImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set the named parameter's value list for this filter.  Used
 * in conjunction with IN-style filter criteria.
 *
 * @param name   The parameter's name.
 * @param values The values to be expanded into an SQL IN list.
 * @return This FilterImpl instance (for method chaining).
 */
public Filter setParameterList(String name, Collection values) throws HibernateException  {
	// Make sure this is a defined parameter and check the incoming value type
	if ( values == null ) {
		throw new IllegalArgumentException( "Collection must be not null!" );
	}
	Type type = definition.getParameterType( name );
	if ( type == null ) {
		throw new HibernateException( "Undefined filter parameter [" + name + "]" );
	}
	if ( !values.isEmpty() ) {
		Class elementClass = values.iterator().next().getClass();
		if ( !type.getReturnedClass().isAssignableFrom( elementClass ) ) {
			throw new HibernateException( "Incorrect type for parameter [" + name + "]" );
		}
	}
	parameters.put( name, values );
	return this;
}
 
Example #5
Source File: FilterKey.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs a number of FilterKey instances, given the currently enabled filters
 *
 * @param enabledFilters The currently enabled filters
 *
 * @return The filter keys, one per enabled filter
 */
public static Set<FilterKey> createFilterKeys(Map<String,Filter> enabledFilters) {
	if ( enabledFilters.size() == 0 ) {
		return null;
	}
	final Set<FilterKey> result = new HashSet<FilterKey>();
	for ( Filter filter : enabledFilters.values() ) {
		final FilterKey key = new FilterKey(
				filter.getName(),
				( (FilterImpl) filter ).getParameters(),
				filter.getFilterDefinition().getParameterTypes()
		);
		result.add( key );
	}
	return result;
}
 
Example #6
Source File: HibernateTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteWithThreadBoundAndParameterizedFilter() {
	Filter filter = mock(Filter.class);
	given(session.isOpen()).willReturn(true);
	given(session.enableFilter("myFilter")).willReturn(filter);
	hibernateTemplate.setAllowCreate(false);
	hibernateTemplate.setFilterName("myFilter");

	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
	try {
		final List l = new ArrayList();
		l.add("test");
		Filter f = hibernateTemplate.enableFilter("myFilter");
		assertTrue("Correct filter", f == filter);
	}
	finally {
		TransactionSynchronizationManager.unbindResource(sessionFactory);
	}
	InOrder ordered = inOrder(session);
	ordered.verify(session).getEnabledFilter("myFilter");
	ordered.verify(session).enableFilter("myFilter");
}
 
Example #7
Source File: HibernateTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteWithThreadBoundAndParameterizedExistingFilter() {
	Filter filter = mock(Filter.class);
	given(session.isOpen()).willReturn(true);
	given(session.enableFilter("myFilter")).willReturn(filter);
	hibernateTemplate.setAllowCreate(false);
	hibernateTemplate.setFilterName("myFilter");

	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
	try {
		final List l = new ArrayList();
		l.add("test");
		Filter f = hibernateTemplate.enableFilter("myFilter");
		assertTrue("Correct filter", f == filter);
	}
	finally {
		TransactionSynchronizationManager.unbindResource(sessionFactory);
	}
	verify(session).getEnabledFilter("myFilter");
}
 
Example #8
Source File: HibernateTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Filter enableFilter(String filterName) throws IllegalStateException {
	Session session = getSessionFactory().getCurrentSession();
	Filter filter = session.getEnabledFilter(filterName);
	if (filter == null) {
		filter = session.enableFilter(filterName);
	}
	return filter;
}
 
Example #9
Source File: ReactiveHQLQueryPlan.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ReactiveHQLQueryPlan(
		String hql,
		boolean shallow,
		Map<String, Filter> enabledFilters,
		SessionFactoryImplementor factory, EntityGraphQueryHint entityGraphQueryHint) {
	super( hql, shallow, enabledFilters, factory, entityGraphQueryHint );
}
 
Example #10
Source File: ReactiveHQLQueryPlan.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ReactiveHQLQueryPlan(
		String hql,
		boolean shallow,
		Map<String, Filter> enabledFilters,
		SessionFactoryImplementor factory) {
	super( hql, shallow, enabledFilters, factory );
}
 
Example #11
Source File: ReactiveHQLQueryPlan.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ReactiveHQLQueryPlan(
		String hql,
		String collectionRole,
		boolean shallow,
		Map<String, Filter> enabledFilters,
		SessionFactoryImplementor factory,
		EntityGraphQueryHint entityGraphQueryHint) {
	super( hql, collectionRole, shallow, enabledFilters, factory, entityGraphQueryHint );
}
 
Example #12
Source File: HibernateTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Filter enableFilter(String filterName) throws IllegalStateException {
	Session session = obtainSessionFactory().getCurrentSession();
	Filter filter = session.getEnabledFilter(filterName);
	if (filter == null) {
		filter = session.enableFilter(filterName);
	}
	return filter;
}
 
Example #13
Source File: HibernateTemplate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Filter enableFilter(String filterName) throws IllegalStateException {
	Session session = getSessionFactory().getCurrentSession();
	Filter filter = session.getEnabledFilter(filterName);
	if (filter == null) {
		filter = session.enableFilter(filterName);
	}
	return filter;
}
 
Example #14
Source File: HibernateTemplate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Filter enableFilter(String filterName) throws IllegalStateException {
	Session session = getSessionFactory().getCurrentSession();
	Filter filter = session.getEnabledFilter(filterName);
	if (filter == null) {
		filter = session.enableFilter(filterName);
	}
	return filter;
}
 
Example #15
Source File: HibernateTemplate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Filter enableFilter(String filterName) throws IllegalStateException {
	Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
	Filter filter = session.getEnabledFilter(filterName);
	if (filter == null) {
		filter = session.enableFilter(filterName);
	}
	return filter;
}
 
Example #16
Source File: HibernateTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Filter enableFilter(String filterName) throws IllegalStateException {
	Session session = obtainSessionFactory().getCurrentSession();
	Filter filter = session.getEnabledFilter(filterName);
	if (filter == null) {
		filter = session.enableFilter(filterName);
	}
	return filter;
}
 
Example #17
Source File: FilterImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Set the named parameter's value for this filter.
 *
 * @param name The parameter's name.
 * @param value The value to be applied.
 * @return This FilterImpl instance (for method chaining).
 * @throws IllegalArgumentException Indicates that either the parameter was undefined or that the type
 * of the passed value did not match the configured type.
 */
public Filter setParameter(String name, Object value) throws IllegalArgumentException {
	// Make sure this is a defined parameter and check the incoming value type
	// TODO: what should be the actual exception type here?
	Type type = definition.getParameterType( name );
	if ( type == null ) {
		throw new IllegalArgumentException( "Undefined filter parameter [" + name + "]" );
	}
	if ( value != null && !type.getReturnedClass().isAssignableFrom( value.getClass() ) ) {
		throw new IllegalArgumentException( "Incorrect type for parameter [" + name + "]" );
	}
	parameters.put( name, value );
	return this;
}
 
Example #18
Source File: LoadQueryInfluencers.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Map<String,Filter> getEnabledFilters() {
	// First, validate all the enabled filters...
	//TODO: this implementation has bad performance
	for ( Filter filter : enabledFilters.values() ) {
		filter.validate();
	}
	return enabledFilters;
}
 
Example #19
Source File: FilterImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the named parameter's value for this filter.
 *
 * @param name The parameter's name.
 * @param value The value to be applied.
 * @return This FilterImpl instance (for method chaining).
 * @throws IllegalArgumentException Indicates that either the parameter was undefined or that the type
 * of the passed value did not match the configured type.
 */
public Filter setParameter(String name, Object value) throws IllegalArgumentException {
	// Make sure this is a defined parameter and check the incoming value type
	// TODO: what should be the actual exception type here?
	Type type = definition.getParameterType( name );
	if ( type == null ) {
		throw new IllegalArgumentException( "Undefined filter parameter [" + name + "]" );
	}
	if ( value != null && !type.getReturnedClass().isAssignableFrom( value.getClass() ) ) {
		throw new IllegalArgumentException( "Incorrect type for parameter [" + name + "]" );
	}
	parameters.put( name, value );
	return this;
}
 
Example #20
Source File: SessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Map getEnabledFilters() {
	errorIfClosed();
	checkTransactionSynchStatus();
	// First, validate all the enabled filters...
	//TODO: this implementation has bad performance
	Iterator itr = enabledFilters.values().iterator();
	while ( itr.hasNext() ) {
		final Filter filter = (Filter) itr.next();
		filter.validate();
	}
	return enabledFilters;
}
 
Example #21
Source File: SessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Filter enableFilter(String filterName) {
	errorIfClosed();
	checkTransactionSynchStatus();
	FilterImpl filter = new FilterImpl( factory.getFilterDefinition(filterName) );
	enabledFilters.put(filterName, filter);
	return filter;
}
 
Example #22
Source File: SQLExtractor.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
/**
 * Get the underlying SQL generated by the provided JPA query.
 *
 * @param query JPA query
 * @return the underlying SQL generated by the provided JPA query
 */
public static String from(Query query) {
    AbstractQueryImpl abstractQuery = query.unwrap(AbstractQueryImpl.class);
    SessionImplementor session = ReflectionUtils.getFieldValue(abstractQuery, "session");
    String[] sqls = session.getFactory()
        .getQueryPlanCache()
        .getHQLQueryPlan(abstractQuery.getQueryString(), false, Collections.<String, Filter>emptyMap())
        .getSqlStrings();

    return sqls.length > 0 ? sqls[0] : null;
}
 
Example #23
Source File: SQLExtractor.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
/**
 * Get the underlying SQL generated by the provided JPA query.
 *
 * @param query JPA query
 * @return the underlying SQL generated by the provided JPA query
 */
public static String from(Query query) {
    AbstractQueryImpl abstractQuery = query.unwrap(AbstractQueryImpl.class);
    SessionImplementor session = ReflectionUtils.getFieldValue(abstractQuery, "session");
    String[] sqls = session.getFactory()
        .getQueryPlanCache()
        .getHQLQueryPlan(abstractQuery.getQueryString(), false, Collections.<String, Filter>emptyMap())
        .getSqlStrings();

    return sqls.length > 0 ? sqls[0] : null;
}
 
Example #24
Source File: SQLExtractor.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
/**
 * Get the underlying SQL generated by the provided JPA query.
 *
 * @param query JPA query
 * @return the underlying SQL generated by the provided JPA query
 */
public static String from(Query query) {
    AbstractQueryImpl abstractQuery = query.unwrap(AbstractQueryImpl.class);
    SessionImplementor session = ReflectionUtils.getFieldValue(abstractQuery, "session");
    String[] sqls = session.getFactory()
        .getQueryPlanCache()
        .getHQLQueryPlan(abstractQuery.getQueryString(), false, Collections.<String, Filter>emptyMap())
        .getSqlStrings();

    return sqls.length > 0 ? sqls[0] : null;
}
 
Example #25
Source File: HibernateTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Filter enableFilter(String filterName) throws IllegalStateException {
	Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
	Filter filter = session.getEnabledFilter(filterName);
	if (filter == null) {
		filter = session.enableFilter(filterName);
	}
	return filter;
}
 
Example #26
Source File: HibernateTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteWithThreadBoundAndParameterizedExistingFilter() {
	Filter filter = mock(Filter.class);
	given(session.enableFilter("myFilter")).willReturn(filter);
	hibernateTemplate.setFilterNames("myFilter");

	final List l = new ArrayList();
	l.add("test");
	Filter f = hibernateTemplate.enableFilter("myFilter");
	assertTrue("Correct filter", f == filter);

	verify(session).getEnabledFilter("myFilter");
}
 
Example #27
Source File: HibernateTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteWithThreadBoundAndParameterizedFilter() {
	Filter filter = mock(Filter.class);
	given(session.enableFilter("myFilter")).willReturn(filter);
	hibernateTemplate.setFilterNames("myFilter");

	final List l = new ArrayList();
	l.add("test");
	Filter f = hibernateTemplate.enableFilter("myFilter");
	assertTrue("Correct filter", f == filter);

	InOrder ordered = inOrder(session);
	ordered.verify(session).getEnabledFilter("myFilter");
	ordered.verify(session).enableFilter("myFilter");
}
 
Example #28
Source File: HibernateTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Filter enableFilter(String filterName) throws IllegalStateException {
	Session session = getSessionFactory().getCurrentSession();
	Filter filter = session.getEnabledFilter(filterName);
	if (filter == null) {
		filter = session.enableFilter(filterName);
	}
	return filter;
}
 
Example #29
Source File: AbstractHibernateDAO.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
protected void disableTenantFilter(Session session) {
	Filter filter = session.getEnabledFilter(TENANT_FILTER_NAME);
	if (filter != null) {
		session.disableFilter(TENANT_FILTER_NAME);
	}
}
 
Example #30
Source File: AbstractHibernateDAO.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
protected void enableTenantFilter(Session session, String tenantId) {
	Filter filter = session.enableFilter(TENANT_FILTER_NAME);
	filter.setParameter("tenant", tenantId);
}