Hibernate不创建联接表 [英] Hibernate doesn't create join table

查看:70
本文介绍了Hibernate不创建联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个以前从未发生过的问题.我有ManyToMany关系,并且Hibernate不创建联接表.它仅创建实体表.我在MySQL上使用SpringDataJpa.

I am encountering a problem which never happened to me before. I have ManyToMany relationship and Hibernate doesn't create the join table. It creates only entity tables. I am using SpringDataJpa with 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

StackTrace:

  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

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

Any ideas what could be the cause?

推荐答案

事实证明,由于 Doctors 主键扩展了允许的大小,Hibernate并未创建联接表.但是,它只能创建 Doctors 表.因此,为了修复它,我必须限制id的大小.我已经这样更改了医生 id:

It turns out that Hibernate didn't create join table because of Doctors primary key extending allowed size. It was however, able to create Doctors table alone. So in order to fix it I had to limit id size. I've changed Doctors id like this:

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

现在,连接表可以由Hibernate创建.

And now join table can be created by Hibernate.

这篇关于Hibernate不创建联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆