将Hibernate SQL显示到控制台(Spring)

编程入门 行业动态 更新时间:2024-10-23 02:03:17
本文介绍了将Hibernate SQL显示到控制台(Spring)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用spring 3,hibernate 4.我试图按照本教程 www.mkyong/hibernate/hibernate-display-generated-sql-to-console-show_sql-format_sql-and-use_sql_comments/ ,但我的hibernate配置不同:

I'm working with spring 3, hibernate 4. I'm trying to follow this tutorial www.mkyong/hibernate/hibernate-display-generated-sql-to-console-show_sql-format_sql-and-use_sql_comments/, but my hibernate configuration is different:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:jee="www.springframework/schema/jee" xmlns:mvc="www.springframework/schema/mvc" xmlns:beans="www.springframework/schema/beans" xmlns:context="www.springframework/schema/context" xmlns:tx="www.springframework/schema/tx" xsi:schemaLocation="www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.2.xsd www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx-3.2.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.2.xsd www.springframework/schema/jee www.springframework/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.apachemons.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>

而show_sql,format_sql和use_sql_comments属性不能以这种方式工作。我得到这个异常:

And the properties show_sql, format_sql and use_sql_comments are not working this way. I get this exception:

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'show_sql' of bean class [org.apachemons.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?

有没有办法用bean的定义来实现教程??

Is there anyway to achieve the tutorial with the definition of the bean??

推荐答案

show_sql不是org.apachemons.dbcp.BasicDataSource的一个属性。你必须在会话工厂配置中定义它。 like this

show_sql not a property of org.apachemons.dbcp.BasicDataSource . You have to define it in session factory configuration. like this

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

更多推荐

将Hibernate SQL显示到控制台(Spring)

本文发布于:2023-10-27 15:57:19,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   Hibernate   SQL   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!