hibernate初學在配置好一切之後,運行然後報錯,接下來問度娘,最後解決。。。
首先看報錯
[org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@6cea706c] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate:
create table NEWS (
ID integer not null auto_increment,
TITLE varchar(255),
AUTHOR varchar(255),
DATE date,
primary key (ID)
) type=InnoDB
Caused by: com.mysql.jdbc.exceptions.jdbc4.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 'type=InnoDB' at line 7
WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
Hibernate:
insert
into
NEWS
(TITLE, AUTHOR, DATE)
values
(?, ?, ?)
WARN: SQL Error: 1146, SQLState: 42S02
ERROR: Table 'hibtest.news' doesn't exist
裏面有建表語句:
Hibernate:
create table NEWS (
ID integer not null auto_increment,
TITLE varchar(255),
AUTHOR varchar(255),
DATE date,
primary key (ID)
) type=InnoDB
報錯也說了
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 7
我們這裏出來問題,這是mysql5.1以前的寫法,5.1之後變化如下:
engine = InnoDB
現在找出問題所在,我們只要修改hibernate.cfg.xml中的方言配置就可以了,不需要修改其他的
正確的做法如下:
org.hibernate.dialect.MySQL5InnoDBDialect