I am attempting to create an entity which has a @OneToMany
relationship with another entity. The problem I am having is when I use @JoinColumn(name = "QuizID")
I get an error Cannot resolve column 'QuizID'
.
As far as I understand the owning side needs a reference to the foreign key, being my Quiz.java
, in particular it's id
field. How do I reference this from @JoinColumn
? How do I know what the column for id
is called? I have tried id
, quizid
, Quiz_id
, quizId
etc etc.
Here is a snippet from the two @Entity
classes with the problem
@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
Here is an example from a tutorial I've been learning from that I'm struggling to understand.
I am trying to understand the annotations for Spring and JPA, particularly @JoinColumn
and @Column
. In this example
@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<>();
}
How why is the @JoinColumn
in Tweet
class name attribute UserID
? I understand it needs to be a column name from the User
entity table (is that right?), but in entity User
how do I know what the column names are? It just has a field for id
, how does that become a column name UserID
?
I have been looking at this SO post but when I follow the answer guide I don't have any options to "Assign data sources", it is the column beside entityManagerFactory
is empty, or says "Missing data source".
My 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()
}
The implementation 'org.springframework.boot:sweb'
was from the tutorial but caused an error and I couldn't find any info on this dependency, others said it was "malformed", the H2 database loaded correctly and I could access it via localhost without this.
My 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
As far as I can tell I'm using disk-based storage option for H2, not in-memory.