我正在尝试使用log4j将生成的SQL查询导出到控制台。使用Spring 4和Hibernate 4。
servlet-context.xml:
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="configLocation" value="classpath:hibernate.cfg.xml" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="connection.autocommit">false</property>
</session-factory>
当我调用以下方法时,控制台不会生成任何信息:
List<Comment> listComment = (List<Comment>) sessionFactory.getCurrentSession()
.createCriteria(Comment.class).list();
有什么建议吗?
==============================%===== ================
我认为问题出在log4j.xml文件中。顺便说一句,我正在使用STS的Spring MVC项目。 Spring 4.3.2 + Hibernate 4.2.5 + Tomcat 8.0 + log4j 1.2.15。
的log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Application Loggers -->
<logger name="com.hersbitcloud.cancercloud">
<level value="info" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<logger name="org.hibernate">
<level value="info" />
</logger>
<logger name="org.hibernage.SQL">
<level value="debug" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
Hibernate生成的日志是:
INFO : org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
INFO : org.hibernate.Version - HHH000412: Hibernate Core {4.3.5.Final}
INFO : org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
INFO : org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
INFO : org.hibernate.cfg.Configuration - HHH000044: Configuring from URL: file:/E:/OneDrive/PostDoc/SuperDr/Code/Spring/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/CancerCloud/WEB-INF/classes/hibernate.cfg.xml
INFO : org.hibernate.cfg.Configuration - HHH000041: Configured SessionFactory: null
INFO : org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
INFO : org.hibernate.engine.transaction.internal.TransactionFactoryInitiator - HHH000399: Using default transaction strategy (direct JDBC transactions)
INFO : org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 2663 ms
甚至'sessionFactory.getCurrentSession()。createCriteria'也不会生成任何SQL语句。
我应该在log4j.xml中添加什么内容吗?
答案 0 :(得分:0)
I got everything to work by changing
<logger name="org.hibernate">
<level value="info" />
</logger>
into
<logger name="org.hibernate">
<level value="all" />
</logger>
答案 1 :(得分:-1)
根据我的知识,这可能是配置,
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>