向控制台显示Hibernate SQL(Spring)
我正在使用Spring3和Hibernate4。我试图遵循本教程,但我的hibernate配置不同:向控制台显示Hibernate SQL(Spring),sql,spring,hibernate,debugging,Sql,Spring,Hibernate,Debugging,我正在使用Spring3和Hibernate4。我试图遵循本教程,但我的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.springframewor
<?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>
有没有办法用bean的定义来实现教程???最简单的方法可能是将以下记录器设置为调试:
org.hibernate.SQL
如果使用log4j,请在类路径根上查找/创建log4j.properties文件并添加
log4j.logger.org.hibernate.SQL=DEBUG
有关log4j属性的更多信息,请参见此处:显示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>
org.hibernate.dial.h2方言
线
org.hibernate.transaction.jdbc事务工厂
真的
更新
当我使用JEE 8和JBoss EAP时,我在添加这一行后设法获得了SQL:
-Dorg.jboss.as.logging.per部署=false
在“VM参数”的末尾(服务器选项卡->JBoss属性->打开lauch配置)。这些是Hibernate属性。您可以在hibernate.cfg.xml(如果有)、persistence.xml(如果使用JPA)中设置它们,或者直接将它们提供给构建hibernate SessionFactory的Spring factory bean,如LocalSessionFactoryBean?我没有hibernate.cfg.xml和persistence.xml。你能举个例子吗?。我仍在学习..可能重复的问题不是重复的问题,因为我遵循的教程对我不起作用,但在尝试@ashish chaurasia的解决方案后,你给我的链接有意义,因为使用的是gerrytan的解决方案。这是我问题的答案,但是@gerrytan的解决方案是互补的。我会投赞成票,因为这很有帮助,但是@ashish chaurasia的解决方案是我问题的答案。无论如何,谢谢你;)这似乎是一个更好的答案,而被接受的答案对我来说并不适用
<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>