San*_*ani 2 java spring jpa foreign-keys
我有一个注册课程,其中有分数列表。我试图引用 Score 类,但得到以下结果:
通过 JDBC 语句执行 DDL“更改表分数添加约束...外键(enrollment_id)引用注册(id)”时出错 原因:java.sql.SQLException:无法打开引用的表“注册”
运行应用程序后,在数据库中创建了 Score 表,但缺少 Enrollment 表,并且 Score 的 enrollment_id 列不是外键。
我怎么解决这个问题?我尝试用 @Cache 进行注释并忽略表的双重创建,但没有成功。我正在使用MySQL。
@Entity
@Table(name = "enrollment")
public class Enrollment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id")
public Student student;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date", nullable = false)
public Date date;
@Column(name = "rank", nullable = false)
public int rank;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "specialty_id", referencedColumnName = "id")
public Specialty specialty;
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "files", columnDefinition = "BLOB", nullable = false)
private byte[] files;
@OneToMany(mappedBy = "enrollment", fetch = FetchType.LAZY)
public List<Score> scores;
@Column(name = "state", length = 50, nullable = false)
public String state;
}
@Entity
@Table(name = "score")
public class Score {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "score_forming_object_id", referencedColumnName = "id")
private ScoreFormingObject scoreFormingObject;
@Column(name = "score", nullable = false)
private double score;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "enrollment_id")
private Enrollment enrollment;
}
Run Code Online (Sandbox Code Playgroud)
/sf/answers/4502175541/ @Rajib Garai 的答案对我有用。我已将 spring.jpa.properties.hibernate.globally_quoted_identifiers=true 添加到 application.properties 并解决了问题。
| 归档时间: |
|
| 查看次数: |
11308 次 |
| 最近记录: |