Spring JPA外键不是在mysql中创建的

时间:2018-03-26 18:47:39

标签: mysql spring hibernate jpa spring-data

我是Spring和Hibernate的新手。当我创建一个表时,没有创建外键。有人可以帮忙!

这是我的实体。 用户

Imports......

@Entity
@Table(name = "application_user")
public class ApplicationUser {

@Id
@GeneratedValue
private Integer id;
@Column(length=40)
private String userName;
@Column(length=100)
private String password;
private int houseNumber;
@Column(length=40)
private String street;
@Column(length=20)
private String city;
@Column(length=20)
private String county;
@Column(length=9)
private String postcode;
@Column(length=60)
private String email;
@Column(length=13)
private String telePhone;

@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
private List<Roles> roles;

Setters and getters.....

角色

Imports.......

@Entity
@Table(name="roles")
public class Roles {

@Id
@GeneratedValue
public Integer id;  
public String name;

Setters and getters.......

当我运行应用程序时,会创建表,但是没有创建外键。 休眠生成的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 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 Workbench中的表时,没有外键约束。但是,如果我在查询窗口中运行SQL脚本,则会创建外键约束。为什么在hibernate创建表时没有创建外键?我已经尝试了两个具有相同结果的MySql服务器。

有没有人有任何想法?

0 个答案:

没有答案