【问题标题】:Getting SQL script from Hibernate update从 Hibernate 更新获取 SQL 脚本
【发布时间】:2011-02-23 15:45:32
【问题描述】:

我正在寻找在 Hibernate 自动更新表时获取 SQL 更新脚本的方法。

我仅在开发环境中使用hibernate.hbm2ddl.auto=update,并且我需要更新生产表的 SQL 脚本。

我希望这些 txt 格式的 SQL 脚本用于修订和潜在的编辑。

如何做到这一点? 感谢您的建议。

【问题讨论】:

    标签: java sql hibernate tomcat


    【解决方案1】:

    有一些建议和一般性讨论here

    简而言之,您可以打开日志记录(到标准输出):

    hibernate.show_sql=true
    

    或者,如果您使用 log4j,您可以将其添加到您的 log4j.properties 文件中:

    log4j.logger.org.hibernate.SQL=DEBUG
    

    这两种方法都将输出带有参数的 Hibernate 准备好的语句(因此参数值本身不是内联的)。为了解决这个问题,您可以使用像 P6Spy 这样的拦截器。详情请见here

    【讨论】:

      【解决方案2】:

      org.hibernate.cfg.Configuration 类有方法:

      public java.lang.String[] generateSchemaUpdateScript( Dialect, DatabaseMetadata)
      


      生成所需更新脚本的内容。

      我刚刚在 grails 中实现了这个:

      configuration = new DefaultGrailsDomainConfiguration(
                          grailsApplication: grailsApplication,
                          properties: props) 
      //this extends hibernate config
      
      Connection c = SessionFactoryUtils.getDataSource(sessionFactory).getConnection(props.'hibernate.connection.username', props.'hibernate.connection.password')
      <br/>md = new DatabaseMetadata(c, DialectFactory.buildDialect(props.'hibernate.dialect'))
      
      configuration.generateSchemaUpdateScript(DialectFactory.buildDialect(props.'hibernate.dialect'), md)
      )
      check SchemaExport script in grails, for further information, it uses hibernate to generate schema.
      


      (我不得不将其实现为服务,因为我们有外部域模型)

      【讨论】:

        猜你喜欢
        • 2014-07-02
        • 2020-03-07
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-29
        • 2014-09-27
        相关资源
        最近更新 更多