java - 如何在 Spring Boot 中为只有外键的表映射/创建实体类
问题描述
我的数据库(Postgres)中有以下表格questions
:responses
和question_response
. 问题和响应表之间存在多对多关系,我为这两种关系创建了实体类。question_respone
我现在必须为没有任何主键的表创建一个实体映射。
我已经阅读了有关使用@IdClass
or的信息@EmbeddedId
,但是,我不确定如何使用这些注释映射两个不同类中的主键的两个外键。
笔记:
在实施评论中提到的更改后更新实体
谢谢!
问题.sql
CREATE TABLE questions(
id BIGSERIAL PRIMARY KEY,
question VARCHAR(255)
);
响应.sql
CREATE TABLE responses(
id BIGSERIAL PRIMARY KEY,
response VARCHAR(255)
);
question_respone.sql #
CREATE TABLE question_response(
question_id bigint REFERENCES questions ON DELETE CASCADE,
response_id bigint REFERENCES responses ON DELETE CASCADE,
PRIMARY KEY ( question_id, response_id)
);
问题.java
@Entity
@Table(name = "questions")
public class Question{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="qid_seq")
@SequenceGenerator(name = "qid_seq", sequenceName="questions_id_seq")
@Column(name = "id")
private Long id;
@Column(name = "questionText")
private String questionText;
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval = true)
private List<QuestionResponse> responses;
public Question() {}
public Question(String questionText) {
super();
this.questionText = questionText;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
public List<QuestionResponse> getResponses() {
return responses;
}
}
QuestionResponse.java
@Entity
@Table(name = "question_response")
public class QuestionResponse {
@Id
@ManyToOne
private Question question;
@Id
@ManyToOne
private Response response;
public QuestionResponse() {
super();
}
public QuestionResponse(Question question, Response response) {
super();
this.question= question;
this.response = response;
}
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
}
响应.java
@Entity
@Table(name = "responses")
public class Response {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="rid_seq")
@SequenceGenerator(name = "rid_seq", sequenceName="questions_id_seq")
@Column(name = "id")
private Long id;
@Column(name = "responseText")
private String responseText;
@OneToMany(mappedBy = "response", cascade = CascadeType.ALL, orphanRemoval = true)
private List<QuestionResponse> question;
public Response() {}
public Response(String responseText) {
super();
this.responseText = responseText;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getResponseText() {
return responseText;
}
public void setResponseText(String responseText) {
this.responseText = responseText;
}
public List<QuestionResponse> getQuestion() {
return question;
}
}
# WildFly 控制台#
13:54:49,581 ERROR [org.springframework.boot.SpringApplication] (ServerService Thread Pool -- 86) Application run failed: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Invocation of init method failed; nested exception is org.hibernate.AnnotationException:
No identifier specified for entity: com.poc.questionnarie.QuestionResponse
解决方案
推荐阅读
- android - 设备错误:警告:意外的“-prop”值(“emu.uuid=520cf82a-fcb6-4b6a-b5c4-386e635c8ba6”),仅支持“qemu.*”属性
- three.js - Three.js 中的相机“扭曲”
- firebase - 尝试从 Firestore 获取数据但收到此错误
- angular - 将全局使用的库模块导入 Angular 项目模块
- python - 熊猫融化功能从宽到长
- java - 从 BufferedReader 接收数据时延迟图形显示
- html - 在 Google 表格中提交表单后重定向
- android - Flutter 全屏通知,如 WhatsApp 通话
- flutter - 如果我一个一个地测试它,颤振测试就通过了,但是当我一起运行它时失败了
- matplotlib - Matplotlib 分段错误 Mac OS 11(Big Sur)