这个问题似乎重复了,但我还没有找到解决方案。
我使用的是spring引导数据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键的努力的组合,但失败了。
发布于 2019-08-22 02:39:17
在application.properties中:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect发布于 2018-08-01 01:33:41
根据@JBNizet的评论,我使用了错误的方言:)
org.hibernate.dialect.MySQL55Dialect发布于 2019-12-05 06:42:51
确保:
<property name="dialect">org.hibernate.dialect.MySQL55Dialect</property>在您的hibernate.cfg.xml中
https://stackoverflow.com/questions/51618040
复制相似问题