Search code examples
javaspringhibernatespring-securityspring-jdbc

Skipping creation of a specific table [Spring] [Hibernate]


I'm creating some simple spring project (spring security) with a configuration file determining some simple values like table names etc. I defined there a boolean field REQUIRED_ACTIVATION for determining if new user have to activate his account with activation link sent for example via mail.

public static final boolean REQUIRED_ACTIVATION = true;

If I set REQUIRED_ACTIVATION value to false user is active immediately after registration. I've got defined entity which contains data for activation links:

@Entity
@Table(name = 'user_activation_link')
public class UserActivationLink {
   [...]
}

When REQUIRED_ACTIVATION is set to false I'm not using this class anywhere but the table is being created in database. Is there any solution for determining if table will be created dependent on value of REQUIRED_ACTIVATION?


Solution

  • You need to do something like this to exclude the tables you don't want to create in the database.

    • Implement the SchemaFilterProvider and the SchemaFilter interfaces
    • In the SchemaFilter implementation, add an if condition to includeTable so that it returns false for the table that you don’t want to create
    • Add hibernate.properties to the classpath and define hibernate.hbm2ddl.schema_filter_provider to point to the SchemaFilterProvider implementation
    hibernate.hbm2ddl.schema_filter_provider=com.your.package.Provider
    

    And the also:

    package com.your.package;
    
    import org.hibernate.boot.model.relational.Namespace;
    import org.hibernate.boot.model.relational.Sequence;
    import org.hibernate.mapping.Table;
    import org.hibernate.tool.schema.spi.SchemaFilter;
    import org.hibernate.tool.schema.spi.SchemaFilterProvider;
    
    public class Provider implements SchemaFilterProvider {
    
        @Override
        public SchemaFilter getCreateFilter() {
            return MySchemaFilter.INSTANCE;
        }
    
        @Override
        public SchemaFilter getDropFilter() {
            return MySchemaFilter.INSTANCE;
        }
    
        @Override
        public SchemaFilter getMigrateFilter() {
            return MySchemaFilter.INSTANCE;
        }
    
        @Override
        public SchemaFilter getValidateFilter() {
            return MySchemaFilter.INSTANCE;
        }
    }
    
    class MySchemaFilter implements SchemaFilter {
    
        public static final MySchemaFilter INSTANCE = new MySchemaFilter();
    
        @Override
        public boolean includeNamespace(Namespace namespace) {
            return true;
        }
    
        @Override
        public boolean includeTable(Table table) {
            if (//REQUIRED_ACTIVATION==true && table.getName() is the table you want to exclude){
                return false;
            }
            return true;
        }
    
        @Override
        public boolean includeSequence(Sequence sequence) {
            return true;
        }
    }