Defining Hibernate 5 FilterDefinition by code

212 views Asked by At

We have a project using Hibernate 3.6.10, and now we are planning to upgrade the Hibernate version to 5.6.0. One problem we have found is in old Hibernate it was possible to register FilterDefinition classes by code, but now I canoot find anyway to do that. So my question is how to register FilterDefinitions by code (not resolved from annotations or XML configs).

1

There are 1 answers

0
Guillaume On

I've not tried it but it seems to be possible to programmatically add a filter definition into the metadata used to build the session factory:

private SessionFactory bootStrapSessionFactory(Configuration configuration) {
    ServiceRegistry serviceRegistry = configuration.getStandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties())
            .build();

    Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
    FilterDefinition filterDefinition = ...
    metadata.getFilterDefinitions().put(key, filterDefinition);
    
    return metadata.buildSessionFactory();
}

I'm not quite sure why you need to build filters programmatically but the above code seems a bit like a hack