5

似乎 org.hibernate.cfg.Configuration 对象可用于通过调用 validateSchema 方法以编程方式执行验证。但是,此方法需要 dialect 和 databaseMetadata 对象。我正在使用 Spring,我可以从 Spring 上下文中获取 AnnotationSessionFactoryBean 对象。到目前为止,我有以下代码:

    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);

我在正确的轨道上吗?ConnectionHelper 层次结构在包外不可见,因此我无法以这种方式获取连接对象,以构造 databaseMetadata。我该如何实施?

编辑:我想我已经取得了一些进展。有一个 SchemaValidator 类。代码现在如下所示:

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

但是,现在我收到以下错误:

org.hibernate.HibernateException:找不到用于配置的本地数据源 - 必须在 LocalSessionFactoryBean 上设置“数据源”属性

4

2 回答 2

4

最后,当使用 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;
        }
    });

}
}
于 2010-02-26T15:55:57.520 回答
1

我找到了一个更简单的解决方案来调用适用于我的休眠映射验证: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(); 
    }
}
于 2012-01-19T14:16:57.117 回答