未在mysql中创建Spring JPA外键
我不熟悉春天和冬眠。创建表时,没有创建外键。有人能帮忙吗 这里是我的实体。 使用者 当我运行应用程序时,表被创建,但是,没有创建外键。 hibernate生成的SQL是:未在mysql中创建Spring JPA外键,mysql,spring,hibernate,jpa,spring-data,Mysql,Spring,Hibernate,Jpa,Spring Data,我不熟悉春天和冬眠。创建表时,没有创建外键。有人能帮忙吗 这里是我的实体。 使用者 当我运行应用程序时,表被创建,但是,没有创建外键。 hibernate生成的SQL是: drop table if exists application_user; drop table if exists application_user_roles; drop table if exists hibernate_sequence; drop table if exists roles; create tab
drop table if exists application_user;
drop table if exists application_user_roles;
drop table if exists hibernate_sequence;
drop table if exists roles;
create table application_user (
id integer not null,
city varchar(20),
county varchar(20),
email varchar(60),
house_number integer not null,
password varchar(100),
postcode varchar(9),
street varchar(40),
tele_phone varchar(13),
user_name varchar(40),
primary key (id)
) ;
create table application_user_roles (
application_user_id integer not null,
roles_id integer not null
) ;
create table hibernate_sequence (next_val bigint);
insert into hibernate_sequence values ( 1 );
insert into hibernate_sequence values ( 1 );
create table roles (
id integer not null,
name varchar(255),
primary key (id)
);
alter table application_user_roles add constraint UK_bn9l8a2gm8lytmn3c2cnbxrxm unique (roles_id);
alter table application_user_roles add constraint FK13sbcqjpwynfdukkr8i1xfyj1
foreign key (roles_id) references roles (id);
alter table application_user_roles add constraint FK9hwva08h4u671cqxpexx1dx7i
foreign key (application_user_id) references application_user (id)
当我查看MySql工作台中的表时,没有外键约束。但是如果我在查询窗口中运行SQL脚本,就会创建外键约束。为什么hibernate创建表时不创建外键?我试过两台MySql服务器,结果都是一样的
有人有什么想法吗?你有为
应用程序\用户\角色创建的实体吗
表?没有,我没有这个实体。Hibernate自动生成链接表。我不理解的是Hibernate生成的SQL是正确的,应该在数据库中生成正确的表。事实并非如此。
Imports.......
@Entity
@Table(name="roles")
public class Roles {
@Id
@GeneratedValue
public Integer id;
public String name;
Setters and getters.......
drop table if exists application_user;
drop table if exists application_user_roles;
drop table if exists hibernate_sequence;
drop table if exists roles;
create table application_user (
id integer not null,
city varchar(20),
county varchar(20),
email varchar(60),
house_number integer not null,
password varchar(100),
postcode varchar(9),
street varchar(40),
tele_phone varchar(13),
user_name varchar(40),
primary key (id)
) ;
create table application_user_roles (
application_user_id integer not null,
roles_id integer not null
) ;
create table hibernate_sequence (next_val bigint);
insert into hibernate_sequence values ( 1 );
insert into hibernate_sequence values ( 1 );
create table roles (
id integer not null,
name varchar(255),
primary key (id)
);
alter table application_user_roles add constraint UK_bn9l8a2gm8lytmn3c2cnbxrxm unique (roles_id);
alter table application_user_roles add constraint FK13sbcqjpwynfdukkr8i1xfyj1
foreign key (roles_id) references roles (id);
alter table application_user_roles add constraint FK9hwva08h4u671cqxpexx1dx7i
foreign key (application_user_id) references application_user (id)