java 如何使 hbm2ddl schemaExport 将架构记录到标准输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3829597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:34:36  来源:igfitidea点击:

How to make hbm2ddl schemaExport to log schema to stdout?

javahibernateormhbm2ddl

提问by yegor256

A quote from persistence.xml:

引自persistence.xml

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <properties>
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="create" />
        ...
    </properties>
</persistence-unit>

This is what I see in log output:

这是我在日志输出中看到的:

Sep 30, 2010 12:03:43 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: Running hbm2ddl schema export
Sep 30, 2010 12:03:43 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: exporting generated schema to database
Sep 30, 2010 12:03:43 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: schema export complete

But I don't see the schema (SQL) exported itself. How to get this information out of Hibernate (3.5.6-Final)?

但我没有看到模式 (SQL) 本身导出。如何从 Hibernate (3.5.6-Final) 中获取这些信息?

回答by Pascal Thivent

Activate the logging of the org.hibernate.tool.hbm2ddlcategory to DEBUG.

激活org.hibernate.tool.hbm2ddl类别的日志记录到DEBUG



Update:Here is a simplified logback.xml(I'm using logback as logging backend):

更新:这是一个简化的logback.xml(我使用 logback 作为日志后端):

<configuration scan="true">

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
  </appender>

  <!-- ### log just the SQL ### -->
  <logger name="org.hibernate.SQL" level="DEBUG"/>

  <!-- ### log JDBC bind parameters ### -->
  <logger name="org.hibernate.type" level="TRACE"/>

  <logger name="org.hibernate.tool.hbm2ddl" level="DEBUG"/>

  <root level="ERROR">
    <appender-ref ref="STDOUT"/>
  </root>
</configuration>

Adapt it if you are using log4j (you'll find working configuration here on SO).

如果您使用的是 log4j,请对其进行调整(您会在 SO 上找到工作配置)。

回答by scheffield

Just in case you stumble upon this using Spring Boot. You can configure the following in your application.yml:

以防万一您在使用Spring Boot时偶然发现了这一点。您可以在您的application.yml:

spring.jpa:
  hibernate.ddl-auto: create-drop
logging.level:               
  org.hibernate.tool.hbm2ddl: DEBUG
  org.hibernate.SQL: DEBUG   
  org.hibernate.type: TRACE  

回答by Thorben Stangenberg

Here is a simplified log4j.xmlconfiguration.

这是一个简化的log4j.xml配置。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n"/>
        </layout>
    </appender>
    <!-- ### log just the SQL ### -->
    <logger name="org.hibernate.SQL">
        <level value="DEBUG" />
    </logger>
    <!-- ### log JDBC bind parameters ### -->
    <logger name="org.hibernate.type">
        <level value="TRACE" />
    </logger>
    <!-- ### log Hibernate model to schema tool ### -->
    <logger name="org.hibernate.tool.hbm2ddl">
        <level value="DEBUG" />
    </logger>
    <root>
        <level value="WARN"/>
        <appender-ref ref="CA"/>
    </root>
</log4j:configuration>