2013-08-27 6 views
7

Ich arbeite mit Feder 3, Hibernate 4. Ich versuche, dieses Tutorial http://www.mkyong.com/hibernate/hibernate-display-generated-sql-to-console-show_sql-format_sql-and-use_sql_comments/ zu folgen, aber meine Hibernate Konfiguration ist anders:Anzeige Hibernate SQL-Konsole (Frühling)

<?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> 

und die Eigenschaften show_sql , format_sql und use_sql_comments funktionieren nicht so. Ich bekomme diese Ausnahme:

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? 

Gibt es sowieso, um das Tutorial mit der Definition der Bohne zu erreichen ??

+0

Das sind Hibernate-Eigenschaften. Sie können sie entweder in hibernate.cfg.xml (falls vorhanden), persistence.xml (bei Verwendung von JPA) oder direkt an eine Spring-Factory-Bean senden, die eine Hibernate-SessionFactory wie LocalSessionFactoryBean erstellt. – superEb

+0

Wie kann ich das letzte tun, was du gesagt hast? Ich habe weder hibernate.cfg.xml noch persistence.xml. Können Sie bitte ein Beispiel veröffentlichen? Ich lerne immer noch .. – kiduxa

+1

möglich Duplikat von [Abfragezeichenfolge im Ruhezustand mit Parameterwerten drucken] (http://stackoverflow.com/questions/1710476/print-query-string-in-hibernate-with-parameter-values) – stevedbrown

Antwort

19

show_sql keine Eigenschaft von org.apache.commons.dbcp.BasicDataSource. Sie müssen es in der Konfiguration der Sitzungs-Factory definieren. so

<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> 
+0

Dies ist die Antwort auf meine Frage, aber @ Gerrytans Lösung ist komplementär. – kiduxa

11

Der einfachste Ansatz ist wahrscheinlich folgende Logger auf DEBUG zu setzen:

org.hibernate.SQL 

Wenn Sie log4j verwenden, finden/eine log4j.properties Datei auf Ihrem Classpath root erstellen und fügen Sie

log4j.logger.org.hibernate.SQL=DEBUG 

See hier für weitere Informationen über log4j Eigenschaften: http://logging.apache.org/log4j/1.2/manual.html

+0

Ich gebe eine Upvote, weil es hilfreich war, aber @ashish Chaurasia Lösung ist die Antwort auf meine Frage. Danke trotzdem;) – kiduxa

+0

Das scheint eine bessere Antwort zu sein und die angenommene Antwort funktioniert nicht für mich. –

+0

beste und einfache Antwort – VJS

Verwandte Themen