Hibernate不会创建外键约束

时间:2018-07-31 16:27:14

标签: java hibernate spring-data-jpa

问题似乎重复,但是我还没有找到解决方法。

我正在使用Spring Boot数据jpa + mysql,这是我的课程:

@Entity
@Getter
@Setter
@Table(name = "Question")
public class Question {

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

  @Column(length = 128)
  private String name;

  @OneToMany(mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<QuestionAnswerMapping> questionAnswerMapping;
}

@Entity
@Getter
@Setter
@Table(name = "Answer")
public class Answer {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int id;

  @Column(length = 255)
  private String displayText;

  @OneToMany(mappedBy = "answer")
  private List<QuestionAnswerMapping> questionAnswerMapping;
}

@Entity
@Table(name = "Question_Answer_Mapping")
public class QuestionAnswerMapping {

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

  @ManyToOne
  @JoinColumn(name="questionId", nullable = false /*, referencedColumnName="id" , foreignKey=@ForeignKey(name="FK_Question", value=ConstraintMode.CONSTRAINT)*/)
  /*    @JoinColumns(value = { @JoinColumn(name="questionId",referencedColumnName="id",foreignKey=@ForeignKey(value=ConstraintMode.CONSTRAINT)) }) */
  private Question question;

  @ManyToOne
  @JoinColumn(name="answerId", nullable = false /*, referencedColumnName="id", foreignKey=@ForeignKey(name="FK_Answer", value=ConstraintMode.CONSTRAINT)*/)
  /*    @JoinColumns(value = { @JoinColumn(name="answerId",referencedColumnName="id",foreignKey=@ForeignKey(value=ConstraintMode.CONSTRAINT)) }) */
  private Answer answer;

}

和application.yml

--- 
spring:
  profiles: local

  datasource:
    url: jdbc:mysql://localhost:3306/test?useSSL=false
    username: root

  jpa:
    generate-ddl: true
    show-sql: true
    hibernate:
      naming:
        physical-strategy:     org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect
        ddl-auto: create-drop

这些是我在ORM创建表之后复制的create语句。

CREATE TABLE `question` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


CREATE TABLE `answer` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `displayText` varchar(255) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


CREATE TABLE `question_answer_mapping` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `answerId` int(11) NOT NULL,
  `questionId` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `FKpjg76y4ofqmvfmbujphnqyq1y` (`answerId`),
  KEY `FK2mbyguxt74rwhv1n1t11wi3fl` (`questionId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

所有注释掉的部分都是我在失败的数据库架构上创建FK密钥的努力的组合。

3 个答案:

答案 0 :(得分:1)

每个@JBNizet评论,我使用了错误的方言:)

org.hibernate.dialect.MySQL55Dialect

答案 1 :(得分:1)

在application.properties中:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect

答案 2 :(得分:0)

确保:

let tree = Node(1, Node(2, Leaf, Leaf), Node(3, Node(4, Leaf, Leaf), Leaf))
tree

在您的hibernate.cfg.xml中

相关问题