如果spring.jpa.hibernate.ddl-auto = update,则从hibernate中记录sql

问题描述 投票:0回答:1

我有一个spring boo应用程序,如果我在应用程序属性中设置spring.jpa.hibernate.ddl-auto=update,我似乎无法在日志中看到SQL。

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

我不明白为什么它在一个案例中工作而不在另一个案例中,我不想每次部署时都删除数据库。

对于日志属性,我设置它们就像这样

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
sql spring hibernate jpa migration
1个回答
1
投票

我从不让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("--------------------------------------------------------------------------------------------------");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.