【Hibernate】创建表时不能生成外键

3 篇文章 0 订阅
2 篇文章 0 订阅

问题


	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'




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值