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


#1

Hello,

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.

[1] https://docs.jboss.org/hibernate/orm/5.3/javadocs/


#2

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.


#3

Hello,

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.


#4

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.


#5

Ok, understood! Thank you!