Skip to content

Instantly share code, notes, and snippets.

@barorion
Last active December 19, 2015 21:08
Show Gist options
  • Select an option

  • Save barorion/6017731 to your computer and use it in GitHub Desktop.

Select an option

Save barorion/6017731 to your computer and use it in GitHub Desktop.

Revisions

  1. barorion renamed this gist Jul 17, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. barorion created this gist Jul 17, 2013.
    38 changes: 38 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    package com.ravello.management.server.data;

    import javax.inject.Inject;

    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaValidator;
    import org.springframework.context.ApplicationContext;
    import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    public class SchemaValidator {

    @Inject
    private ApplicationContext applicationContext;

    private Configuration configuration;

    private final Logger log = LoggerFactory.getLogger(SchemaValidator.class);

    // a workaround bean to validate schema, since setting hbm2ddl.auto=validate causes the sessionFactory bean to be
    // loaded before the Flyway bean
    public void validate() {
    try {
    LocalSessionFactoryBean localSessionFactoryBean = (LocalSessionFactoryBean) applicationContext
    .getBean("&sessionFactory"); // gets the original instance, not the bean; assuming your session factory bean is 'sessionFactory'

    configuration = localSessionFactoryBean.getConfiguration();

    SchemaValidator hibernateSchemaValidator = new SchemaValidator(configuration);
    hibernateSchemaValidator.validate();
    log.info("successfully validated schema");
    }
    catch (Exception e) {
    throw new RuntimeException("Error while validating db schema; fix db and restart application", e);
    }
    }
    }