hbm2ddl.auto设置为update时不能工作排查方法!

问题描述:

在调试hibernate的时候发现设置hbm2ddl.auto设置为update是不能工作,eclipse中报如下错误:

Hibernate: insert into news (title, content) values (?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.ericsson.ewanbao.News]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)

...................

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate.news' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

...............


问题分析:

经检查数据库发现,news表确实创建不成功,可见hbm2ddl.auto设置为update没有能够建立表


解决步骤:

1.把mysql的general-log打开,这样可以跟踪所有的sql语句;

2.再次执行程序,在general-log的最后发现如下sql语句:

   3 Query SHOW FULL TABLES FROM `hibernate` LIKE 'news'
   3 Query create table news (id integer not null auto_increment, title varchar(255), content varchar(255), primary key (id)) type=InnoDB
   3 Query SHOW FULL TABLES FROM `hibernate` LIKE 'PROBABLYNOT'
   3 Query SET autocommit=0
   3 Query SET autocommit=1
   3 Query SET autocommit=0
   3 Query insert into news (title, content) values ('???????', '???????????!')
   3 Query SHOW FULL TABLES FROM `hibernate` LIKE 'PROBABLYNOT'

3.可以发现hibernate已经发送了见表语句给数据库,为什么建表没有成功了?尝试拷贝这些建表语句到sql控制台执行,得到如下结果:

mysql> create table news (id integer not null auto_increment, title varchar(255), content varchar(255), primary key (id)
) type=InnoDB;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
 for the right syntax to use near 'type=InnoDB' at line 1

4.可以看到是sql语句错误,经分析发现是hibernate.dialect的设置错误,原先设置为org.hibernate.dialect.MySQLInnoDBDialect,应该修改为org.hibernate.dialect.MySQL5InnoDBDialect

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd " > <context:property-placeholder location="classpath:application.properties"></context:property-placeholder> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.aiearth.drone.dao,com.aiearth.pojo" /> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.hbm2ddl.auto"> update </prop> --> <prop key="hibernate.dialect"> <!-- org.hibernate.spatial.dialect.postgis.PostgisDialect--> org.hibernate.spatial.dialect.postgis.PostgisPG10Dialect <!-- org.hibernate.dialect.PostgreSQLDialect--> </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.use_sql_comments">false</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="hibernate.query.escape_like_parameter">false</prop> <prop key="hibernate.query.sql.escape-identifier">false</prop> <prop key="hibernate.logger.org.hibernate.mapping">DEBUG</prop> </props> </property> </bean> <bean id="dataSource" class="com.aiearth.drone.security.DataSource.UmspscDataSource"> <property name="driverClassName" value="org.postgresql.Driver"/> <property name="jdbcUrl" value="${drone.jdbc.url}?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&stringtype=unspecified"/> <property name="username" value="${drone.jdbc.username}"/> <property name="password" value="${drone.jdbc.password}"/> <property name="connectionTimeout" value="30000"/> <property name="idleTimeout" value="180000"/> <property name="maximumPoolSize" value="30"/> <property name="minimumIdle" value="10"/> <property name="maxLifetime" value="1200000"/> <property name="connectionTestQuery" value="SELECT 1"/> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" primary="true"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com.aiearth..*.service..*Service.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> </beans>这样配对吗,如果不对请给我完整的修改后配置
最新发布
07-31
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值