3

I am working on a Spring MVC app using Hibernate. Following is my dispatcher servlet code:

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <context:component-scan base-package="com.example.abc" />
    <tx:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:/MySqlDS" />
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="jndiDataSource" />
        <property name="packagesToScan" value="com.example.abc"></property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

I do not have any Hibernate config file as Hibernate mapping is done in model classes. I need to print SQL generated from Hibernate statements.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vishal Suri
  • 447
  • 2
  • 12
  • 32

4 Answers4

5

Update your sessionFactory as follows:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="jndiDataSource" />
    <property name="packagesToScan" value="com.example.abc"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
S. Pauk
  • 5,208
  • 4
  • 31
  • 41
  • I have added property in sessionFactory. How can I now print sql now? – Vishal Suri Apr 07 '15 at 11:34
  • doesn't it appear in your log? – S. Pauk Apr 07 '15 at 11:36
  • you may need to check your logging level as well as it is suggested here: http://stackoverflow.com/questions/7074481/hibernate-not-showing-sql-statement-even-with-sql-show-true/7077244#7077244 – S. Pauk Apr 07 '15 at 11:38
  • @vishi under the answer score there's a gray check, click it – S. Pauk Apr 07 '15 at 11:48
  • @vishi I've noticed you have already asked quite a few questions on SO and most part of them is unanswered. Could you review your old questions and mark proper answers? You could read more about answering here: http://stackoverflow.com/help/accepted-answer – S. Pauk Apr 07 '15 at 11:52
0

You can see hibernate issued SQL by using the debug log level for org.hibernate.SQL.

See the documentation (version 4.3) for more informations

0

Create hibernate.cfg.xml file.... and add

<!--hibernate.cfg.xml -->
<property name="show_sql">true</property>

More info here.

If you don't want to create the xml file... use Log4J.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
-1

You can try this

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="jndiDataSource" />
        <property name="packagesToScan" value="com.example.abc"></property>
        <property name="hibernateProperties">
        <props>            
            <prop key="hibernate.show_sql">true</prop>            
        </props>
    </property>
    </bean>

You can provide other hibernate configuration properties. Also you may want to use properties file for those configurations and inject those with ${} syntax in the xml configuration. For e.g.,

<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
Shailendra
  • 8,874
  • 2
  • 28
  • 37