2013-08-27 12 views
7

pracuję ze sprężyną 3, hibernacji 4. Staram się śledzić ten tutorial http://www.mkyong.com/hibernate/hibernate-display-generated-sql-to-console-show_sql-format_sql-and-use_sql_comments/, ale moja konfiguracja hibernacji jest inna:Wyświetlacz Hibernate SQL Console (wiosna)

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

i właściwości show_sql , format_sql i use_sql_comments nie działają w ten sposób. Dostaję ten wyjątek:

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? 

Czy istnieje sposób na uzyskanie samouczka z definicją fasoli?

+0

Są to właściwości hibernacji. Możesz ustawić je w hibernate.cfg.xml (jeśli masz), persistence.xml (jeśli używasz JPA) lub przekazać je bezpośrednio do fasoli Spring Spring, która buduje Hibernate SessionFactory, np. LocalSessionFactoryBean. – superEb

+0

Jak mogę zrobić ostatnią rzecz, którą powiedziałeś? Nie mam pliku hibernate.cfg.xml ani persistence.xml. Czy możesz wysłać przykład, proszę ?. Nadal uczę się .. – kiduxa

+1

możliwy duplikat [Wydrukuj ciąg zapytania w hibernacji z wartościami parametrów] (http://stackoverflow.com/questions/1710476/print-query-string-in-hibernate-with-parameter-values) – stevedbrown

Odpowiedz

19

show_sql nie jest własnością org.apache.commons.dbcp.BasicDataSource. Musisz to zdefiniować w fabrycznej konfiguracji sesji. podoba się to

<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

To jest odpowiedź na moje pytanie, ale rozwiązanie @ gerrytan jest komplementarne. – kiduxa

11

Najprostsze podejście prawdopodobnie jest ustawiony następujący rejestrator do debug:

org.hibernate.SQL 

Jeśli używasz log4j, wybrać/utworzyć plik log4j.properties na korzeniu ścieżce klas i dodać

log4j.logger.org.hibernate.SQL=DEBUG 

See tutaj, aby uzyskać więcej informacji o właściwościach log4j: http://logging.apache.org/log4j/1.2/manual.html

+0

Dam kurs, ponieważ był pomocny, ale rozwiązanie @ chishaya jest odpowiedzią na moje pytanie. Dzięki i tak;) – kiduxa

+0

To wydaje się być lepszą odpowiedzią, a zaakceptowana odpowiedź nie działa dla mnie. –

+0

najlepsza i prosta odpowiedź – VJS