Hibernate连接Mysql无法自动创建表多种原因分析
1.错误原因:hbm文件中定义的元素含有sql的关键字,例如:
<property name="name"></property> <property name="birthday"></property> <property name="from"></property>
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 1at 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)<property name="hbm2ddl.auto">create-drop</property>
# The default storage engine that will be used when create new tables whendefault-storage-engine=INNODB
<property name="hbm2ddl.auto">create</property>
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'test.hzhu_user' doesn't existat 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)