代码之家  ›  专栏  ›  技术社区  ›  Miksel

如果是spring,则从hibernate记录sql。jpa。冬眠ddl自动=更新

  •  1
  • Miksel  · 技术社区  · 6 年前

    我有一个spring boo应用程序,如果设置 spring.jpa.hibernate.ddl-auto=update 在应用程序属性中。

    我觉得奇怪的是,如果我将属性设置为create drop,就可以看到生成的SQL spring.jpa.hibernate.ddl-auto=create-drop

    我不明白为什么它在一种情况下有效,而在另一种情况下无效,我不想每次部署时都删除数据库。

    至于日志属性,我将其设置为

    spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL94Dialect
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.hibernate.use-new-id-generator-mappings=true
    spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
    spring.jpa.properties.hibernate.format_sql=true
    
    logging.level.org.hibernate=INFO
    #this line shows the sql statement in the logs
    logging.level.org.hibernate.tool.hbm2ddl=trace
    logging.level.org.hibernate.tool.hbm2ddl.SchemaUpdate = trace
    #this line shows sql values in the logs
    logging.level.org.hibernate.type.descriptor.sql=trace
    logging.level.org.hibernate.SQL=DEBUG
    logging.level.org.hibernate.type=TRACE
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   AKrush95    5 年前

    我从不让hibernate直接迁移模式。下面的类将所有必要的DDL更改转储到日志和文件中。 我建议以后将SQL放入Flyway迁移中。

    public class SchemaUpdateService implements InitializingBean {
    
    
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd--HHmm");
    
        private static final Logger log = LoggerFactory.getLogger(SchemaUpdateService.class);
    
        // Simply here to ensure a fully build up hibernate stuff.
        @Autowired
        LocalContainerEntityManagerFactoryBean factoryBean;
    
    
        @SuppressWarnings({"deprecation", "unused"})
        @Override
        public void afterPropertiesSet() throws Exception {
    
            String fileName = System.getProperty("java.io.tmpdir") + "/database-migration-" + formatter.format(LocalDateTime.now()) + ".sql";
    
            Metadata metadata = HibernateInfoHolder.getMetadata();
            SessionFactoryServiceRegistry serviceRegistry = HibernateInfoHolder.getServiceRegistry();
            org.hibernate.tool.hbm2ddl.SchemaUpdate schemaUpdate = new org.hibernate.tool.hbm2ddl.SchemaUpdate();
            schemaUpdate.setDelimiter(";");
            schemaUpdate.setOutputFile(fileName);
            schemaUpdate.setFormat(true);
    
            log.warn("--------------------------------------------------------------------------------------------------");
            log.warn("Starting SCHEMA MIGRATION lookup, please add the following SQL code (if any) to a flyway migration");
            log.warn("Working on schema: " +  factoryBean.getJpaPropertyMap().get("schema_name") );
            schemaUpdate.execute(  EnumSet.of(TargetType.SCRIPT, TargetType.STDOUT), metadata, serviceRegistry);
    
            File file = new File(fileName);
            if (file.exists() && file.length() != 0) {  // migrations present.
                log.warn("Migrations also written to: " + fileName);
            } else if (file.exists()) {  // delete empty files
                log.warn("No migrations");
                file.delete();
            }
    
            log.warn("END OF SCHEMA MIGRATION lookup");
            log.warn("--------------------------------------------------------------------------------------------------");
        }
    }