1. 程式人生 > >【Hibernate】建立表時不能生成外來鍵

【Hibernate】建立表時不能生成外來鍵

問題


	alter table t_student drop constraint FK4B907570A5F18255
	drop table t_classes if exists
	drop table t_student if exists
	create table t_classes (id integer not null, name varchar(255), primary key (id))
	create table t_student (id integer not null, name varchar(255), classesid integer not null, primary key (id))
	alter table t_student add constraint FK4B907570A5F18255 foreign key (classesid) references t_classes
	16:21:49,775 ERROR SchemaExport:274 - Unsuccessful: alter table t_student add constraint FK4B907570A5F18255 foreign key (classesid) references t_classes
16:21:49,778 ERROR SchemaExport:275 - Cannot add foreign key constraint

原因

       由於mysql使用的5.7.19,而在hibernate中配置的mysql方言是org.hibernate.dialect.HSQLDialect,其不能實現mysql5.0版本以上的部分命令。

解決

       將hibernate中配置mysql方言的org.hibernate.dialect.HSQLDialect改為org.hibernate.dialect.MySQL5InnoDBDialect即可,或者改為通用的org.hibernate.dialect.MySQLDialect也是可以的。


	alter table t_student drop foreign key FK4B907570A5F18255
	drop table if exists t_classes
	16:52:09,458  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
	16:52:09,460  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_classes'
	drop table if exists t_student
	16:52:09,462  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
	16:52:09,463  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_classes'
	16:52:09,463  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
	16:52:09,464  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_student'
	create table t_classes (id integer not null, name varchar(255), primary key (id)) ENGINE=InnoDB
	16:52:09,489  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
	16:52:09,490  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_classes'
	16:52:09,490  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
	16:52:09,490  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_student'
	create table t_student (id integer not null, name varchar(255), classesid integer not null, primary key (id)) ENGINE=InnoDB
	16:52:09,514  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
	16:52:09,515  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_classes'
	16:52:09,515  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
	16:52:09,515  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_student'
	alter table t_student add index FK4B907570A5F18255 (classesid), add constraint FK4B907570A5F18255 foreign key (classesid) references t_classes (id)
	16:52:09,589  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
	16:52:09,589  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_classes'
	16:52:09,590  WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
16:52:09,590  WARN JDBCExceptionReporter:49 - Unknown table 'hibernate_one2many.t_student'