Vishal Suri
Vishal Suri

Reputation: 455

How to print generated SQL in Hibernate in Spring MVC application?

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.

Upvotes: 3

Views: 3678

Answers (4)

Shailendra
Shailendra

Reputation: 9102

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>

Upvotes: -1

S. Pauk
S. Pauk

Reputation: 5318

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>

Upvotes: 5

Jordi Castilla
Jordi Castilla

Reputation: 26961

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.

Upvotes: 0

user180100
user180100

Reputation:

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

Upvotes: 0

Related Questions