如何在Hibernate中通过注释以编程方式验证数据库模式? [英] How to validate database schema programmatically in hibernate with annotations?

查看:101
本文介绍了如何在Hibernate中通过注释以编程方式验证数据库模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  AnnotationSessionFactoryBean factory = null; 
factory =(AnnotationSessionFactoryBean)context.getBean(AnnotationSessionFactory);
配置配置= factory.getConfiguration();

//以下行不起作用,ConnectionHelper层次结构在包外不可见
ConnectionHelper connectionHelper =
新的ManagedConnectionProviderConnectionHelper(factory.getHibernateProperties());

方言dialect = Dialect.getDialect(factory.getHibernateProperties());
连接连接=空;
DatabaseMetadata databaseMetadata = null;
尝试{
databaseMetadata = new DatabaseMetadata(connection,dialect);
} catch(SQLException e){
// TODO自动生成的catch块
e.printStackTrace();
}
configuration.validateSchema(dialect,databaseMetadata);

我在正确的轨道上吗? ConnectionHelper层次结构在包外不可见,所以我无法以这种方式获取连接对象,以便构建数据库元数据。我怎么能实现这一点?



编辑:
我想我已经取得了一些进展。有一个SchemaValidator类。代码现在看起来像这样:

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

Howerver,现在我收到以下错误消息:



org.hibernate.HibernateException:找不到配置的本地DataSource - 必须在LocalSessionFactoryBean上设置'dataSource'属性

解决方案

最后,在使用Spring时,这并不那么简单。我设法扩展AnnotationSessionFactoryBean,如下所示:
$ b $ pre $ public class SchemaValidatingAnnotationSessionFactoryBean extends
AnnotationSessionFactoryBean {

public void validateDatabaseSchema()抛出DataAccessException {
logger.info(为Hibernate SessionFactory验证数据库模式);
HibernateTemplate hibernateTemplate = new HibernateTemplate(
getSessionFactory());
hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER);
hibernateTemplate.execute(new HibernateCallback(){
public Object doInHibernate(会话会话)
抛出HibernateException,SQLException {
Connection con = session.connection();
Dialect方言= Dialect.getDialect(getConfiguration()
.getProperties());
DatabaseMetadata元数据= new DatabaseMetadata(con,dialect);
配置配置= getConfiguration();
configuration.validateSchema(dialect,metadata);
return null;
}
});

}
}


It seems that org.hibernate.cfg.Configuration object can be used to perform validation programmatically, by calling the validateSchema method. However, this method needs dialect and databaseMetadata objects. I am using Spring and I can get a hold of AnnotationSessionFactoryBean object from 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? ConnectionHelper hierarchy is not visible out of the package so I can’t obtain the connection object that way, in order to construct databaseMetadata. How can I implement this?

EDIT: I think I have made some progress. There is a SchemaValidator class. The code now looks like this:

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

Howerver, now I am getting the following error:

org.hibernate.HibernateException: No local DataSource found for configuration - 'dataSource' property must be set on LocalSessionFactoryBean

解决方案

In the end, when using Spring this is not that simple. I managed to do it extending the AnnotationSessionFactoryBean like this:

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

}
}

这篇关于如何在Hibernate中通过注释以编程方式验证数据库模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆