gpt4 book ai didi

mysql - Hibernate 不创建连接表

转载 作者:行者123 更新时间:2023-11-29 07:28:03 27 4
gpt4 key购买 nike

我遇到了一个以前从未发生过的问题。我有 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;
}

应用程序属性:

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

任何想法可能是什么原因?

最佳答案

事实证明,由于 Doctors 主键扩展了允许的大小,Hibernate 没有创建连接表。但是,它能够单独创建 Doctors 表。所以为了修复它,我不得不限制 id 大小。我已经像这样更改了 Doctors id:

@Id
@Column(length = 11)
@NotNull
private String pesel;

现在连接表可以由 Hibernate 创建。

关于mysql - Hibernate 不创建连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910621/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com