首页 > 解决方案 > 使用@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<>();
 
}

为什么@JoinColumnTweet名属性是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 使用基于磁盘的存储选项,而不是在内存中。

标签: javaspringjpaormannotations

解决方案


推荐阅读