Session.createFilter is deprecated in Hibernate ORM 5.3. Is there any alternative?


starting from Hibernate 5.3 Session.createFilter(Object, String) has been deprecated[1] with no real replacement.

What do you think that it could the best approach to take if I would like to filter a given collection and replace the usage of that method?

Thank you in advance.


You can use the @Filter annotation on collections as explained in the User Guide.

Or you can use the @Where annotation if the filtering is not dynamic.


thanks for the response. And what if I’m still not building the mapping with JPA annotations? In my case I’m still building the mapping with an .hbm file. Then at one point in my code I’m doing the following:

session.createFilter(collection,“where language = :language”).setParameter(“language”, language).list();

Is there any workaround for this? Or the only way is by replacing it with a Query?

Thanks again.

Those annotations have an equivalent in HBM mappings. However, HBM mappings are also deprecated. You might want to migrate to annotations, which are better supported anyway.

Other workaround is to write a Query. You can use Criteria API if the query needs to be build dynamically.

Ok, understood! Thank you!

Hi Vlad,

In my project, to replace all createFilter methods , it can be complex even with CriteriaQuery.
Do you know examples, with Criteria?

Following the example below , how i would apply Criteria in easy way, if my collection is
ListDefinition list = getListDefinition(listId); instead a table ?

public Collection getListColumnsOrderedByName (Long listId)

    ListDefinition list = getListDefinition(listId);
    Collection OrderedColumns = wlmDaoHelper.getListDefinitionDao().filter(list.getListColumns(), "order by this.columnName");

// Collection OrderedColumns = wlmDaoHelper.getCurrentTransactionalHibernateSession().filter(list.getListColumns(),
// “order by this.columnName”);

    return OrderedColumns;