如果spring.jpa.hibernate.ddl-auto = update则从休眠日志sql [英] Log sql from hibernate if spring.jpa.hibernate.ddl-auto=update

查看:190
本文介绍了如果spring.jpa.hibernate.ddl-auto = update则从休眠日志sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a spring boo application and I cant seem to see the SQL in the logs if I set spring.jpa.hibernate.ddl-auto=update in application properties.

我觉得很奇怪,如果将属性设置为create-drop spring.jpa.hibernate.ddl-auto=create-drop

What I find weird is that I can see the SQL generated if I set the property to create-drop spring.jpa.hibernate.ddl-auto=create-drop

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

I dont understand why it works in one case and not in the other and I dont want to drop the database everytime I deploy.

就日志记录属性而言,我将其设置为如此

As far as logging properties i have them set like so

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

推荐答案

我从不让休眠直接迁移架构.下列类将所有必要的DDL更改转储到日志和文件中. 我建议之后将SQL进行Flyway迁移.

I never let hibernate migrate the schema directly. The following class dumps all necessary DDL changes to the log and a file. I recommend to put the SQL in a Flyway migration afterwards.

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

这篇关于如果spring.jpa.hibernate.ddl-auto = update则从休眠日志sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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