我遇到了一个以前从未发生过的问题。我有 ManyToMany
关系,Hibernate 不创建连接表。它仅创建实体表。我正在将 SpringDataJpa 与 MySQL 结合使用。
医生实体:
Entity
@Table(name = "doctors")
public class Doctor {
@Id
@Size(min = 11, max = 11)
@NotNull
private String pesel;
@NotNull
private String firstName;
@NotNull
private String lastName;
private Long salary;
@NotNull
private String speciality;
@OneToOne
private Doctor supervisor;
@ManyToMany
@JoinTable(name = "doctors_treatments",
joinColumns = @JoinColumn(name = "pesel", referencedColumnName = "pesel"),
inverseJoinColumns = @JoinColumn(name = "treatment_id", referencedColumnName = "id"))
private Set<Treatment> treatments;
}
治疗实体:
@Table(name = "treatments")
public class Treatment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private String name;
private Long cost;
@ManyToMany(mappedBy = "treatments")
private Set<Doctor> doctors;
}
application.properties:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:33061/sys
堆栈跟踪:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'sys.doctors_treatments' doesn't exist
有什么想法可能是什么原因吗?
最佳答案
事实证明,Hibernate 没有创建连接表,因为 Doctors 主键扩展了允许的大小。然而,它能够单独创建Doctors表。因此,为了解决这个问题,我必须限制 id 大小。我已经更改了Doctors id,如下所示:
@Id
@Column(length = 11)
@NotNull
private String pesel;
现在 Hibernate 可以创建连接表了。
关于mysql - Hibernate不创建连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910621/