How to programmatically check a database schema in sleep mode with annotations?

It seems that the org.hibernate.cfg.Configuration object can be used to programmatically validate validation by calling the validateSchema method. However, this method requires dialect objects and Metadata databases. I use Spring, and I can get the AnnotationSessionFactoryBean object from the Spring context. So far I have the following code:

    AnnotationSessionFactoryBean factory = null;
    factory = (AnnotationSessionFactoryBean) context.getBean("AnnotationSessionFactory");
    Configuration configuration = factory.getConfiguration();

    //the following line does not work, ConnectionHelper hierarchy is not visible outside the package
    ConnectionHelper connectionHelper =  
   new ManagedConnectionProviderConnectionHelper(factory.getHibernateProperties());

    Dialect dialect = Dialect.getDialect(factory.getHibernateProperties());
    Connection connection = null;
    DatabaseMetadata databaseMetadata = null;
    try {
        databaseMetadata = new DatabaseMetadata(connection, dialect);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    configuration.validateSchema(dialect, databaseMetadata);

Am I on the right track? The ConnectionHelper hierarchy is not visible from the package, so I cannot get the connection object in such a way as to create a Metadata database. How to implement this?

EDIT: I think I have made some progress. There is a SchemaValidator class. Now the code is as follows:

AnnotationSessionFactoryBean factory = context.getBean("&AnnotationSessionFactory");
Configuration configuration = factory.getConfiguration();       
SchemaValidator validator = new SchemaValidator(configuration);
validator.validate();       

Howerver, now I get the following error:

org.hibernate.HibernateException: - dataSource LocalSessionFactoryBean

+5
2

, Spring . , AnnotationSessionFactoryBean :

public class SchemaValidatingAnnotationSessionFactoryBean extends
    AnnotationSessionFactoryBean {

public void validateDatabaseSchema() throws DataAccessException {
    logger.info("Validating database schema for Hibernate SessionFactory");
    HibernateTemplate hibernateTemplate = new HibernateTemplate(
            getSessionFactory());
    hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER);
    hibernateTemplate.execute(new HibernateCallback() {
        public Object doInHibernate(Session session)
                throws HibernateException, SQLException {
            Connection con = session.connection();
            Dialect dialect = Dialect.getDialect(getConfiguration()
                    .getProperties());
            DatabaseMetadata metadata = new DatabaseMetadata(con, dialect);
            Configuration configuration = getConfiguration();
            configuration.validateSchema(dialect, metadata);
            return null;
        }
    });

}
}
+4

hibernate, : LocalSessionFactoryBean.validateDatabaseSchema() ( , )

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/some-test-context.xml" })
public class TestMapping {

    @Autowired
    ApplicationContext context; 

    @Test
    public void validateSchema() {
        AnnotationSessionFactoryBean factory = (AnnotationSessionFactoryBean)context
                .getBean("&mySessionFactory");
        factory.validateDatabaseSchema(); 
    }
}
+1

All Articles