日期:2014-05-16  浏览次数:20693 次

Hibernate连接Mysql无法自动创建表多种原因分析
1.错误原因:hbm文件中定义的元素含有sql的关键字,例如:
  <property name="name"></property>
  <property name="birthday"></property>
  <property name="from"></property>

  代码中含有"from"关键字,创建表时抛错如下代码:
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'from) values ('name', '2009-04-02 14:23:59', null)' at line 1
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)


2.错误原因:配置文件中使用了create-drop属性,数据库表其实已经创建,只是又被删除了
<property name="hbm2ddl.auto">create-drop</property>

只要把create-drop替换为create即可

3.错误原因:默认的数据库类型错误。
把mysql中my.ini文件中配置的 MyISAM 变成 INNODB,代码如下,
# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB


4.错误原因;hbm文件中未写错,但是在myeclipse中开发的
myeclipse不能自动识别如下语句,需要在Configuration中手动重新配置一下即可运行;
<property name="hbm2ddl.auto">create</property>

也可能hbm2dll的相对位置在配置文件中过低,导致无法识别(参考其他人提出的原因)

错误代码
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'test.hzhu_user' doesn't exist
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)