[英]About auto create table in Hibernate Java Spring boot application
I have a small project Java Spring Boot, i use Annotation to create field and Table in MySQL, i want to add some other field in a middle table " candidate_tets " to store some other field, then i create a class " CandidateTest ".我有一个小项目 Java Spring Boot,我使用 Annotation 在 MySQL 中创建字段和表,我想在中间表“ candidate_tets ”中添加一些其他字段来存储其他一些字段,然后我创建一个 class“ CandidateTest ”。 But when i add annotation @Id and @GeneratedValue(strategy = GenerationType.IDENTITY) in var " id ", i have a problem, that the AutoIncrement in MySQL and PRIMARY KEY isn't set to column id in MySQL, i don't know the problem in here但是当我在var“ id ”中添加注释@Id和@GeneratedValue(strategy = GenerationType.IDENTITY)时,我遇到了一个问题,即MySQL中的AutoIncrement和PRIMARY KEY未设置为MySQL中的列id,我不知道这里的问题
this is my Candidate class这是我的候选人class
@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 ...
}
this is test class:这是测试class:
@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 ...
this is candidate_test class:这是candidate_test class:
@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 ....
file application.properties文件 application.properties
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
I think it is because of the Serializable interface
try removing that and then re-execute it should work.我认为这是因为Serializable interface
尝试删除它然后重新执行它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.