提问者:小点点

关于Hibernate Java Spring Boot应用程序中的自动创建表


我有一个小项目Java Spring Boot,我使用注释在MySQL中创建字段和表,我想在中间表“candidate_tets”中添加一些其他字段来存储其他字段,然后我创建了一个类“Candidate Test”。但是当我在var “id”中添加注释@Id和@GeneratedValue(策略= GenerationType.IDENTITY)时,我遇到了一个问题,MySQL和PRIMARY KEY中的自动增量未在MySQL中设置为列ID,我不知道这里的问题

这是我的候选人类

@Entity
@Table(name = "candidate")
@SuppressWarnings("serial")
public class Candidate implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private int id;
    @Column
    private String name;
    @Column(nullable = true)
    private int level; // 1 Fresher, 2 Junior, 3 Senior
    @Column(unique = true)
    private String phone;
    @Column(unique = true)
    private String email;
    @Column(nullable = true)
    private String position;// Vi tri ???
    @Column(name = "english_mark", columnDefinition = "DOUBLE DEFAULT 0")
    private Double englishMark;
    @Column(name = "coding_mark", columnDefinition = "DOUBLE DEFAULT 0")
    private Double codingMark;
    @Column(name = "knowledge_mark", columnDefinition = "DOUBLE DEFAULT 0")
    private Double knowledgeMark;
    
    @Column(name = "date_test")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss", iso = ISO.DATE_TIME)
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime dates;
    
    @Column(name = "is_done", columnDefinition = "INT DEFAULT 0")
    private int isDone;

    @ManyToMany(fetch = FetchType.EAGER)
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @JoinTable(name = "candidate_Test", joinColumns = {@JoinColumn(name = "id_candidate")}, 
                inverseJoinColumns = {@JoinColumn(name = "id_test")})
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
    private Set<Test> tests = new HashSet<>();
    
    @Column
    private String avatar;
    
    //Constructor , Getter and Setter ...
    

}

这是测试类:

@Entity
@Table(name = "test")
@JsonIgnoreProperties({"hibernateLazyInitializer"})
@SuppressWarnings("serial")
public class Test implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private int id;
    @Column
    private int subject;
    @Column
    private int level;

    @Column(name = "time")
    @DateTimeFormat(pattern = "HH:mm:ss", iso = ISO.TIME)
    @JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "HH:mm:ss")
    private LocalTime times;
    
    @Column
    private String name;
    
    @Column(name = "code_test", unique = true)
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private String codeTest;

    @ManyToMany
    @JsonIgnore
    @JoinTable(name = "candidate_Test", joinColumns = {@JoinColumn(name = "id_test")},    inverseJoinColumns = {@JoinColumn(name = "id_candidate")})
    private List<Candidate> candidates = new ArrayList<>();

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "test_question",
    joinColumns = {@JoinColumn(name = "id_test")}, inverseJoinColumns = {@JoinColumn(name = "id_question")})
    private Set<Question> questions = new HashSet<>();

//Getter and Setter ...

这是候选测试类:

@Entity
@SuppressWarnings("serial")
@Table(name = "candidate_test")
public class CandidateTest implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private int id;
    @Column(name = "id_candidate")
    private int candidateId;
    @Column(name = "id_test")
    private int testId;
    @Column(name = "marks", columnDefinition = "DOUBLE DEFAULT 0")
    private Double marks;
        //Getter and Setter ....

文件应用程序.属性

spring.datasource.url = jdbc:mysql://db:3306/testingonline?zeroDateTimeBehavior=convertToNull&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username= root
spring.datasource.password= 123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.use-new-id-generator-mappings= false
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
springdoc.api-docs.path=/api-docs
spring.mvc.pathpattern.matching-strategy=ant_path_matcher 
spring.jackson.serialization.write-dates-as-timestamps=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect```


this is MySQL table *candidate_test* and the problem

![enter image description here](https://i.stack.imgur.com/2SWKW.png)

Can you guys give me the reason why the AutoIncrement and PRIMARY KEY is not set in this middle table

共1个答案

匿名用户

我认为这是因为< code > Serializable interface 尝试删除它,然后重新执行它应该工作。