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.

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

5 Comments

I have added property in sessionFactory. How can I now print sql now?
doesn't it appear in your log?
you may need to check your logging level as well as it is suggested here: stackoverflow.com/questions/7074481/…
@vishi under the answer score there's a gray check, click it
@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: stackoverflow.com/help/accepted-answer
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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.