java - 使用@ManyToOne 和@OneToMany,@Column(name = "QuizID") 给出警告“无法解析列 'QuizID'
问题描述
我正在尝试创建一个@OneToMany
与另一个实体有关系的实体。我遇到的问题是当我使用时@JoinColumn(name = "QuizID")
出现错误Cannot resolve column 'QuizID'
。
据我了解,拥有方需要引用外键,即 my Quiz.java
,尤其是它的id
字段。我如何参考这个@JoinColumn
?我怎么知道该列的id
名称?我已经尝试过id
,,,quizid
等等Quiz_id
等等quizId
。
@Entity
这是有问题的两个类的片段
@Component
@Entity(name = "quiz_table")
public class Quiz {
@Id
private long id;
@NotBlank(message = "Title must not be blank")
private String title;
@NotBlank(message = "Text must not be blank")
private String text;
@Size(min = 2)
@NotNull
@OneToMany(mappedBy = "quiz")
private List<Options> options = new ArrayList<>();
@JsonIgnore
@OneToMany
private List<Answer> answer = new ArrayList<>();
public Quiz() {}
// getters and setters ...
@Component
@Entity(name = "options_table")
public class Options {
@Id
@Column(name = "OptionsID")
private long id;
@ManyToOne
@JoinColumn(name = "QuizID") // error here "Cannot resolve column"
private Quiz quiz;
private String option;
public Options() {
}
// ... getters and setters
这是我一直在努力理解的教程中的一个示例。我正在尝试理解 Spring 和 JPA 的注释,尤其是@JoinColumn
和@Column
. 在这个例子中
@Entity
public class Tweet {
@Id
@Column(name = "TweetID")
private long id;
@ManyToOne
@JoinColumn(name = "UserID")
private User user;
}
@Entity
public class User {
@Id
private long id;
@OneToMany(mappedBy = "user")
private List<Tweet> tweets = new ArrayList<>();
}
为什么@JoinColumn
类Tweet
名属性是UserID
什么?我知道它需要是User
实体表中的列名(对吗?),但是在实体User
中我怎么知道列名是什么?它只有一个字段id
,它如何成为列名UserID
?
我一直在看这篇 SO 帖子,但是当我按照答案指南进行操作时,我没有任何“分配数据源”选项,它旁边的列entityManagerFactory
是空的,或者说“缺少数据源”。
我的build.gradle
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'io.github.siaust'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
// New dependencies for a H2 database
// implementation 'org.springframework.boot:sweb'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-validation')
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
来自implementation 'org.springframework.boot:sweb'
教程,但导致错误,我找不到有关此依赖项的任何信息,其他人说它“格式错误”,H2 数据库正确加载,没有这个我可以通过 localhost 访问它。
我的application.properties
#datasource settings
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:file:../quizdb
spring.datasource.username=sa
spring.datasource.password=password
#data settings
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
#console settings
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.jpa.show-sql=true
#changes the path to the database in the browser, default is /h2-console
#spring.h2.console.path=/h2
据我所知,我正在为 H2 使用基于磁盘的存储选项,而不是在内存中。
解决方案
推荐阅读
- javascript - 使用 Javascript 更改 CSS 显示属性但不断消失
- sql - 如何验证配置文件何时分配给用户 Oracle 服务器
- python - 有没有更快的方法来判断两张图片是否不同?
- python - 如何一次按一下而不是按3次按钮?
- php - 自动化 git pull 在具有 cron 和 url 的服务器上不起作用
- c# - 将.cs文件的颜色分配给wpf中的solidcolorbrush
- cmake - Yocto 中未从生成的二进制文件中删除的调试符号
- eclipse - DPDK 开发人员使用哪个 IDE,他们如何调试代码?
- mxgraph - 是否可以更改 Edge 的 endArrow 的颜色?
- python - 在 pyspark 中通过检查加载数据框给了我空的数据框