如何使hbm2ddl schemaExport将模式记录到stdout?

时间:2010-09-30 10:09:25

标签: java hibernate orm hbm2ddl

来自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>

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

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

但我没有看到架构(SQL)导出自己。如何从Hibernate(3.5.6-Final)获取此信息?

3 个答案:

答案 0 :(得分:16)

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


更新:这是一个简化的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>

如果你使用的是log4j,你可以调整它(你可以在SO上找到工作配置)。

答案 1 :(得分:6)

以防万一你使用 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  

答案 2 :(得分:0)

这是一个简化的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>