我有一个spring boo应用程序,如果我在应用程序属性中设置spring.jpa.hibernate.ddl-auto=update
,我似乎无法在日志中看到SQL。
我觉得奇怪的是,如果我将属性设置为create-drop 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
答案 0 :(得分: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("--------------------------------------------------------------------------------------------------");
}
}