Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

mysql - foriegn key constraint not being created

so i use MySQL 8.0 and hibernate 5.4.25 which currently is the most stable version of hibernate,here is the code i wrote to create a foriegn key

@Entity
@Table(name="Instructor")
public class Instructor {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="e_id")
    private int e_id;
    
    @Column(name="name")
    private String name;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "detail_id",referencedColumnName = "d_id", 
    foreignKey = @ForeignKey(name="fk_detail", value = ConstraintMode.PROVIDER_DEFAULT))
    private InstructorDetails detail;
}

and this is my other entity class

@Entity
@Table(name="InstructorDetails")
public class InstructorDetails {

    @OneToOne(mappedBy = "detail",cascade = CascadeType.ALL)
    private Instructor instructor;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "d_id")
    private int d_id;
    
    @Column(name = "age")
    private int age;
    
    @Column(name = "branch")
    private String branch;
    
    @Column(name = "hobby")
    private String hobby;
    
    @Column(name = "dept")
    private String dept;
}

and yes of course i have all the required setters and getters and constructors in the entity classes i have not included them here just so that question remains compact

the problem here is i have included the property show_sql property as true in my hibernate config file which shows me that the following DDL command has been executed

create table Instructor (
       e_id integer not null auto_increment,
        name varchar(255),
        detail_id integer,
        primary key (e_id)
    ) engine=MyISAM

alter table Instructor 
       add constraint fk_detail 
       foreign key (detail_id) 
       references InstructorDetails (d_id)

this means that according to this DDL foreign key with the constraint name "fk_detail" has been created but if i open mysql workbench and check out the DDL command that created the table then the below DDL is shown

CREATE TABLE `instructor` (
  `e_id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `detail_id` int DEFAULT NULL,
  PRIMARY KEY (`e_id`),
  KEY `fk_detail` (`detail_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

as we can see a key "fk_detail" is created but its not a foreign key though because referenced table and column details are missing and i have seen the DDL of other tables where foreign key constraint is created in those cases all the details where available, and besides detail_id is not functioning like a foreign key at all

so how do i resolve this problem, how do i actually create a foreign key using hibernate annotations


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...