【问题标题】:Display Hibernate SQL To Console (Spring)显示休眠 SQL 到控制台(春季)
【发布时间】:2013-08-29 15:21:37
【问题描述】:

我正在使用 spring 3,hibernate 4。我正在尝试遵循本教程 http://www.mkyong.com/hibernate/hibernate-display-generated-sql-to-console-show_sql-format_sql-and-use_sql_comments/,但我的 hibernate 配置不同:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">


  <!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with 
       username root and blank password. Change below if it's not the case -->
 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/project"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
    <property name="validationQuery" value="SELECT 1"/>
     <property name="show_sql" value="true" />
    <property name="format_sql" value="true" />
    <property name="use_sql_comments" value="true" />
  </bean>


</beans>

并且属性 show_sql、format_sql 和 use_sql_cmets 不能以这种方式工作。我得到了这个例外:

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'show_sql' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'show_sql' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

有没有办法用bean的定义实现教程??

【问题讨论】:

  • 这些是 Hibernate 属性。您可以在 hibernate.cfg.xml(如果有)、persistence.xml(如果使用 JPA)中设置它们,或者将它们直接提供给构建 Hibernate SessionFactory 的 Spring 工厂 bean,如 LocalSessionFactoryBean。
  • 我该怎么做你说的最后一件事?我没有hibernate.cfg.xml 也没有persistence.xml。你可以发布一个例子吗?我还在学习..
  • 这不是一个重复的问题,因为我正在关注一个对我不起作用的教程,但是在尝试@ashish chaurasia 的解决方案之后,你给我的链接是有意义的,因为正在使用 gerrytan 的解决方案.

标签: sql spring hibernate debugging


【解决方案1】:

show_sql 不是 org.apache.commons.dbcp.BasicDataSource 的属性。您必须在会话工厂配置中定义它。 像这样

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="data" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

【讨论】:

  • 这是我问题的答案,但@gerrytan 的解决方案是互补的。
【解决方案2】:

最简单的方法可能是将以下记录器设置为调试:

org.hibernate.SQL

如果您使用 log4j,请在您的类路径根目录中查找/创建 log4j.properties 文件并添加

log4j.logger.org.hibernate.SQL=DEBUG

有关 log4j 属性的更多信息,请参见此处:http://logging.apache.org/log4j/1.2/manual.html

【讨论】:

  • 我会投赞成票,因为它很有帮助,但@ashish chaurasia 的解决方案是我问题的答案。无论如何谢谢;)
  • 这似乎是一个更好的答案,而接受的答案对我不起作用。
  • 最佳而简单的答案
【解决方案3】:

当我使用 JEE 8 和 JBoss EAP 时,添加这一行后我设法得到了 SQL:

-Dorg.jboss.as.logging.per-deployment=false

在“VM 参数”的末尾(服务器选项卡 -> JBoss 属性 -> 打开 lauch 配置)。

【讨论】:

    猜你喜欢
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    相关资源
    最近更新 更多