Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用@ManyToOne和@OneToMany,@Column(name=";QuizID";)给出警告;无法解析列';奎齐德';_Java_Spring_Jpa_Orm_Annotations - Fatal编程技术网

Java 使用@ManyToOne和@OneToMany,@Column(name=";QuizID";)给出警告;无法解析列';奎齐德';

Java 使用@ManyToOne和@OneToMany,@Column(name=";QuizID";)给出警告;无法解析列';奎齐德';,java,spring,jpa,orm,annotations,Java,Spring,Jpa,Orm,Annotations,我正在尝试创建一个与另一个实体具有@OneToMany关系的实体。我遇到的问题是,当我使用@JoinColumn(name=“QuizID”)时,我收到一个错误无法解析列'QuizID' 据我所知,拥有方需要引用外键,即我的quick.java,特别是它的id字段。如何从@JoinColumn中引用此内容?我如何知道id的列被称为什么?我试过id,quizid,quick\u id,quizid等等 下面是两个有问题的@Entity类的一个片段 @Component @Entity(name =

我正在尝试创建一个与另一个实体具有
@OneToMany
关系的实体。我遇到的问题是,当我使用
@JoinColumn(name=“QuizID”)
时,我收到一个错误
无法解析列'QuizID'

据我所知,拥有方需要引用外键,即我的
quick.java
,特别是它的
id
字段。如何从
@JoinColumn
中引用此内容?我如何知道
id
的列被称为什么?我试过
id
quizid
quick\u 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 ...
   
这是一个我一直在努力理解的教程中的例子。 我试图理解Spring和JPA的注释,特别是
@JoinColumn
@Column
。在这个例子中

@Entity
public class Tweet {
 
    @Id
    @Column(name = "TweetID")
    private long id;
 
    @ManyToOne
    @JoinColumn(name = "UserID")
    private User user;
}
实现'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使用基于磁盘的存储选项,而不是在内存中。

我真的很难理解这里要问什么。你怎么知道这些列叫什么。。。在您自己的数据库模式中?该模式最初是如何形成的?您可以使用
@JoinColumn.name
覆盖默认的列命名。如果您不关心名称,就不要指定它。使用普通的
@JoinColumn
。此外,如果您尝试了实体映射的多个版本,则数据库文件中的模式可能不再与当前版本兼容(在这种情况下,假设您只是在测试,只需删除
。/quizdb
文件并从头开始)@crizzis My bad,我试图在没有澄清问题的情况下,尽可能多地提供相关信息
@JoinColumn(name=“name”)
在IntelliJ IDEA中的name属性下有一个警告。从那以后,我了解到它不会阻止代码编译或运行,并且看到我的数据库表列名反映了此注释和
@column
@Entity(name=“table_name”)
等中的更改。是的,我最终使用了多个不必要的列并删除了数据库。有没有办法删除和更新这些更改的列?最可靠、生产就绪的方法是将数据库模式更改管理外部化到Flyway或Liquibase之类的工具中。您可以使用JPA提供程序的模式更新模式(如果可用),但它会
@Entity
public class User {
 
    @Id
    private long id;
 
    @OneToMany(mappedBy = "user")
    private List<Tweet> tweets = new ArrayList<>();
 
}
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()
}

#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